Flask-SQLAlchemy model structure

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
I'm seeking a review of my Flask-SQLAlchemy model structure, particularly adherence to DB best practices, such as avoiding duplication, etc.
The models represent the relations between Departments, Teams, and Roles. A more detailed explanation of these has been included.
Model Specification:
Department: A Department represents a collection of Teams. A Department without Teams should be considered valid. On deletion, all Teams associated with the Department should also be deleted.
Team: A Team represents a collection of Roles. A Team without Roles should be considered valid. On deletion, all Roles associated with the Team should also be deleted. A Team may only belong to one department.
Role: A Role may only belong to one Team.
Models:
class Department(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), nullable=False)
teams = db.relationship(
'Team',
backref='department',
cascade='all, delete-orphan',
lazy='dynamic'
)
def __repr__(self):
return '<Department >'.format(self.name)
class Team(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), nullable=False)
roles = db.relationship(
'Role',
backref="team",
cascade="all, delete-orphan",
lazy='dynamic'
)
department_id = db.Column(
db.Integer,
db.ForeignKey('department.id'),
nullable=False
)
def __repr__(self):
return '<Team >'.format(self.name)
class Role(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), nullable=False)
team_id = db.Column(db.Integer, db.ForeignKey('team.id'), nullable=False)
def __repr__(self):
return '<Role : ID >'.format(self.name, self.id)
Questions:
Had I written this as SQL, I would have expected Role to contain both the Department_id and Team_id. Can the same structure be achieved using SQL-Alchemy? Is the current (sub-optimal?) structure symptomatic of an ORM?
Update
The tables this produces are as follows:
Department: ID, Name
Team: ID, Name, department_id
Role: ID, Name, role_id
python flask sqlalchemy orm
add a comment |Â
up vote
2
down vote
favorite
I'm seeking a review of my Flask-SQLAlchemy model structure, particularly adherence to DB best practices, such as avoiding duplication, etc.
The models represent the relations between Departments, Teams, and Roles. A more detailed explanation of these has been included.
Model Specification:
Department: A Department represents a collection of Teams. A Department without Teams should be considered valid. On deletion, all Teams associated with the Department should also be deleted.
Team: A Team represents a collection of Roles. A Team without Roles should be considered valid. On deletion, all Roles associated with the Team should also be deleted. A Team may only belong to one department.
Role: A Role may only belong to one Team.
Models:
class Department(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), nullable=False)
teams = db.relationship(
'Team',
backref='department',
cascade='all, delete-orphan',
lazy='dynamic'
)
def __repr__(self):
return '<Department >'.format(self.name)
class Team(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), nullable=False)
roles = db.relationship(
'Role',
backref="team",
cascade="all, delete-orphan",
lazy='dynamic'
)
department_id = db.Column(
db.Integer,
db.ForeignKey('department.id'),
nullable=False
)
def __repr__(self):
return '<Team >'.format(self.name)
class Role(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), nullable=False)
team_id = db.Column(db.Integer, db.ForeignKey('team.id'), nullable=False)
def __repr__(self):
return '<Role : ID >'.format(self.name, self.id)
Questions:
Had I written this as SQL, I would have expected Role to contain both the Department_id and Team_id. Can the same structure be achieved using SQL-Alchemy? Is the current (sub-optimal?) structure symptomatic of an ORM?
Update
The tables this produces are as follows:
Department: ID, Name
Team: ID, Name, department_id
Role: ID, Name, role_id
python flask sqlalchemy orm
Could you share your DB scheme?
â Mast
Apr 7 at 14:56
1
@Mast, I've added an update to show the tables produced.
â DavidSaucony
Apr 7 at 15:04
You needn't storedepartment_idinRole. You can getdepartment_idof a` Role` withrole.team.department_id.
â stamaimer
May 10 at 3:20
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'm seeking a review of my Flask-SQLAlchemy model structure, particularly adherence to DB best practices, such as avoiding duplication, etc.
The models represent the relations between Departments, Teams, and Roles. A more detailed explanation of these has been included.
Model Specification:
Department: A Department represents a collection of Teams. A Department without Teams should be considered valid. On deletion, all Teams associated with the Department should also be deleted.
Team: A Team represents a collection of Roles. A Team without Roles should be considered valid. On deletion, all Roles associated with the Team should also be deleted. A Team may only belong to one department.
Role: A Role may only belong to one Team.
Models:
class Department(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), nullable=False)
teams = db.relationship(
'Team',
backref='department',
cascade='all, delete-orphan',
lazy='dynamic'
)
def __repr__(self):
return '<Department >'.format(self.name)
class Team(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), nullable=False)
roles = db.relationship(
'Role',
backref="team",
cascade="all, delete-orphan",
lazy='dynamic'
)
department_id = db.Column(
db.Integer,
db.ForeignKey('department.id'),
nullable=False
)
def __repr__(self):
return '<Team >'.format(self.name)
class Role(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), nullable=False)
team_id = db.Column(db.Integer, db.ForeignKey('team.id'), nullable=False)
def __repr__(self):
return '<Role : ID >'.format(self.name, self.id)
Questions:
Had I written this as SQL, I would have expected Role to contain both the Department_id and Team_id. Can the same structure be achieved using SQL-Alchemy? Is the current (sub-optimal?) structure symptomatic of an ORM?
Update
The tables this produces are as follows:
Department: ID, Name
Team: ID, Name, department_id
Role: ID, Name, role_id
python flask sqlalchemy orm
I'm seeking a review of my Flask-SQLAlchemy model structure, particularly adherence to DB best practices, such as avoiding duplication, etc.
The models represent the relations between Departments, Teams, and Roles. A more detailed explanation of these has been included.
Model Specification:
Department: A Department represents a collection of Teams. A Department without Teams should be considered valid. On deletion, all Teams associated with the Department should also be deleted.
Team: A Team represents a collection of Roles. A Team without Roles should be considered valid. On deletion, all Roles associated with the Team should also be deleted. A Team may only belong to one department.
Role: A Role may only belong to one Team.
Models:
class Department(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), nullable=False)
teams = db.relationship(
'Team',
backref='department',
cascade='all, delete-orphan',
lazy='dynamic'
)
def __repr__(self):
return '<Department >'.format(self.name)
class Team(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), nullable=False)
roles = db.relationship(
'Role',
backref="team",
cascade="all, delete-orphan",
lazy='dynamic'
)
department_id = db.Column(
db.Integer,
db.ForeignKey('department.id'),
nullable=False
)
def __repr__(self):
return '<Team >'.format(self.name)
class Role(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), nullable=False)
team_id = db.Column(db.Integer, db.ForeignKey('team.id'), nullable=False)
def __repr__(self):
return '<Role : ID >'.format(self.name, self.id)
Questions:
Had I written this as SQL, I would have expected Role to contain both the Department_id and Team_id. Can the same structure be achieved using SQL-Alchemy? Is the current (sub-optimal?) structure symptomatic of an ORM?
Update
The tables this produces are as follows:
Department: ID, Name
Team: ID, Name, department_id
Role: ID, Name, role_id
python flask sqlalchemy orm
edited Apr 15 at 4:10
Jamalâ¦
30.1k11114225
30.1k11114225
asked Apr 7 at 14:39
DavidSaucony
112
112
Could you share your DB scheme?
â Mast
Apr 7 at 14:56
1
@Mast, I've added an update to show the tables produced.
â DavidSaucony
Apr 7 at 15:04
You needn't storedepartment_idinRole. You can getdepartment_idof a` Role` withrole.team.department_id.
â stamaimer
May 10 at 3:20
add a comment |Â
Could you share your DB scheme?
â Mast
Apr 7 at 14:56
1
@Mast, I've added an update to show the tables produced.
â DavidSaucony
Apr 7 at 15:04
You needn't storedepartment_idinRole. You can getdepartment_idof a` Role` withrole.team.department_id.
â stamaimer
May 10 at 3:20
Could you share your DB scheme?
â Mast
Apr 7 at 14:56
Could you share your DB scheme?
â Mast
Apr 7 at 14:56
1
1
@Mast, I've added an update to show the tables produced.
â DavidSaucony
Apr 7 at 15:04
@Mast, I've added an update to show the tables produced.
â DavidSaucony
Apr 7 at 15:04
You needn't store
department_id in Role. You can get department_id of a` Role` with role.team.department_id.â stamaimer
May 10 at 3:20
You needn't store
department_id in Role. You can get department_id of a` Role` with role.team.department_id.â stamaimer
May 10 at 3:20
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191478%2fflask-sqlalchemy-model-structure%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Could you share your DB scheme?
â Mast
Apr 7 at 14:56
1
@Mast, I've added an update to show the tables produced.
â DavidSaucony
Apr 7 at 15:04
You needn't store
department_idinRole. You can getdepartment_idof a` Role` withrole.team.department_id.â stamaimer
May 10 at 3:20