Skip to content

Add UserRole model with 1:1 relationship to User#26

Open
Copilot wants to merge 5 commits intomasterfrom
copilot/add-user-model
Open

Add UserRole model with 1:1 relationship to User#26
Copilot wants to merge 5 commits intomasterfrom
copilot/add-user-model

Conversation

Copy link
Contributor

Copilot AI commented Jan 30, 2026

Implements role-based access control foundation by adding a UserRole model with a 1:1 relationship to User. The problem statement showed a type mismatch (db.Integer foreign key against db.String(36) primary key) which would fail at runtime.

Changes

  • Created UserRole model following the UserStats pattern:

    • Uses db.String(36) for primary key and foreign key (matches User.id UUID type)
    • Defaults to "User" role
    • Cascade deletes with parent User
  • Updated User model:

    • Added role relationship with uselist=False for 1:1 semantics
  • Added test coverage for creation, relationship bidirectionality, cascade delete, defaults, and type validation

Implementation

class UserRole(db.Model):
    __tablename__ = "user_roles"
    
    id = db.Column(
        db.String(36), db.ForeignKey("users.id", ondelete="CASCADE"), primary_key=True
    )
    role = db.Column(db.String(50), default="User", nullable=False)
    created_at = db.Column(db.DateTime, server_default=db.func.now(), nullable=False)

class User(db.Model):
    # ...
    role = db.relationship(
        "UserRole", backref="user", uselist=False, cascade="all, delete-orphan"
    )

Access pattern: user.role.role returns the role string; user_role.user returns the User instance.

Original prompt

class User(db.Model):
"""
User model for the application. Stores user information such as email, password, and username.

Attributes:
    id (str): Unique identifier for the user.
    email (str): Email address of the user.
    password (str): Hashed password of the user.
    username (str): Username of the user.
    created_at (datetime): Timestamp when the user was created.
    updated_at (datetime): Timestamp when the user was last updated.

Relationships:
    stats (UserStats): One-to-one relationship with UserStats.
"""

__tablename__ = "users"

id = db.Column(db.String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
email = db.Column(db.String(120), unique=True, nullable=False)
username = db.Column(db.String(120), unique=True, nullable=False)
password = db.Column(db.String(256), nullable=False)
created_at = db.Column(db.DateTime, server_default=db.func.now())
updated_at = db.Column(
    db.DateTime, server_default=db.func.now(), onupdate=db.func.now()
)

# ------------
# User relationships
# ------------
stats = db.relationship(
    "UserStats", backref="user", uselist=False, cascade="all, delete-orphan"
)

class UserRole(db.Model):
tablename = "user_roles"

user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False)
role = db.Column(db.String(50), nullable=False)
created_at = db.Column(db.DateTime, default=db.func.current_timestamp())

How to properly create a relation between the User and UserRole models, That is related to issue #22


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits January 30, 2026 21:48
Co-authored-by: karastoyanov <86146020+karastoyanov@users.noreply.github.com>
Co-authored-by: karastoyanov <86146020+karastoyanov@users.noreply.github.com>
Copilot AI changed the title [WIP] Add user model for application Add UserRole model with 1:1 relationship to User Jan 30, 2026
Copilot AI requested a review from karastoyanov January 30, 2026 21:54
Aleksandar Karastoyanov added 2 commits January 31, 2026 00:13
- added class model UserRoles with 1-to-1 relationship with User
- new users get User role by default
- added pytest tests

Signed-off-by: Aleksandar Karastoyanov <a.karastoyanov@utp.bg>
…killforge into copilot/add-user-model

Signed-off-by: Aleksandar Karastoyanov <a.karastoyanov@utp.bg>
@karastoyanov karastoyanov marked this pull request as ready for review January 30, 2026 22:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Add roles to the application with UserRole model (1:1 user relation)

2 participants