Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''The most common relationships are one-to-many relationships. Because relationships are declared before they are established you can use strings to refer to classes that are not created yet (for instance if Person defines a relationship to Address which is declared later in the file).
- Relationships are expressed with the relationship() function. However the foreign key has to be separately declared with the ForeignKey class:'''
- class Person(db.Model):
- id = db.Column(db.Integer, primary_key=True)
- name = db.Column(db.String(50), nullable=False)
- addresses = db.relationship('Address', backref='person', lazy=True)
- class Address(db.Model):
- id = db.Column(db.Integer, primary_key=True)
- email = db.Column(db.String(120), nullable=False)
- person_id = db.Column(db.Integer, db.ForeignKey('person.id'), nullable=False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement