Advertisement
viettiennguyen029

SQLAlchemy

Sep 17th, 2020
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1. '''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).
  2. Relationships are expressed with the relationship() function. However the foreign key has to be separately declared with the ForeignKey class:'''
  3.  
  4.  
  5. class Person(db.Model):
  6.     id = db.Column(db.Integer, primary_key=True)
  7.     name = db.Column(db.String(50), nullable=False)
  8.     addresses = db.relationship('Address', backref='person', lazy=True)
  9.  
  10. class Address(db.Model):
  11.     id = db.Column(db.Integer, primary_key=True)
  12.     email = db.Column(db.String(120), nullable=False)
  13.     person_id = db.Column(db.Integer, db.ForeignKey('person.id'), nullable=False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement