Flask-SQLAlchemy model structure

The name of the pictureThe name of the pictureThe name of the pictureClash 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






share|improve this question





















  • 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_id in Role. You can get department_id of a` Role` with role.team.department_id.
    – stamaimer
    May 10 at 3:20
















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






share|improve this question





















  • 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_id in Role. You can get department_id of a` Role` with role.team.department_id.
    – stamaimer
    May 10 at 3:20












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






share|improve this question













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








share|improve this question












share|improve this question




share|improve this question








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 store department_id in Role. You can get department_id of a` Role` with role.team.department_id.
    – stamaimer
    May 10 at 3:20
















  • 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_id in Role. You can get department_id of a` Role` with role.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















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

Python Lists

Aion

JavaScript Array Iteration Methods