class Post(db.Model): """Represents Proected posts.""" __tablename__ = 'posts' id = db.Column(db.String(45), primary_key=True) title = db.Column(db.String(255)) text = db.Column(db.Text()) publish_date = db.Column(db.DateTime) # Set the foreign key for Post user_id = db.Column(db.String(45), db.ForeignKey('users.id')) comments = db.relationship('Comment', backref='posts', lazy='dynamic') # many to many: posts <==> tags tags = db.relationship( 'Tag', #会告知 SQLAlchemy 该 many to many 的关联保存在 posts_tags 表中 secondary=posts_tags, #声明表之间的关系是双向,帮助手册 help(db.backref)。 # 需要注意的是:在 one to many 中的 backref 是一个普通的对象, # 而在 many to many 中的 backref 是一个 List 对象 backref=db.backref('posts', lazy='dynamic')) def __init__(self, title): self.title = title def __repr__(self): return "<Model Post `{}`>".format(self.title)
class ItemModel(db.Model): __tablename__ = 'items' id_ = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(20)) price = db.Column(db.Float(precision=2)) store_id = db.Column(db.Integer, db.ForeignKey('stores.id_')) store = db.relationship('StoreModel') def __init__(self, name, price, store_id): self.name = name self.price = price self.store_id = store_id def json(self): return { 'name': self.name, 'price': self.price, 'store_id': self.store_id } @classmethod def get_by_name(cls, name): return cls.query.filter_by(name=name).first() def save_to_db(self): db.session.add(self) db.session.commit() def delete_from_db(self): db.session.delete(self) db.session.commit()
class StoreModel(db.Model): __tablename__ = 'stores' id_ = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(20)) # back reference items = db.relationship('ItemModel', lazy = 'dynamic') def __init__(self,name): self.name = name def json(self): return {'name':self.name, 'items':[x.json() for x in self.items.all()]} @classmethod def get_by_name(cls,name): return cls.query.filter_by(name=name).first() def save_to_db(self): db.session.add(self) db.session.commit() def delete_from_db(self): db.session.delete(self) db.session.commit()
class User(db.Model, UserMixin): """ Saving new users data into our database """ id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False) firstname = db.Column(db.String(20), nullable=False) lastname = db.Column(db.String(20), nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(60), nullable=False) garments = db.relationship('Garment', backref='seller', lazy=True) messages = db.relationship('Message', backref='sender', lazy=True) def __repr__(self): return f"User('{self.username}', '{self.email}')" def set_password(self, secret): self.password = generate_password_hash(secret) def check_password(self, secret): return check_password_hash(self.password, secret)
class User(db.Model): __tablename__ = 'users' id = db.Column(db.String(45), primary_key=True) username = db.Column(db.String(255)) password = db.Column(db.String(255)) #会在 SQLAlchemy 中创建一个虚拟的列,该列会与 Post.user_id (db.ForeignKey) 建立联系。这一切都交由 SQLAlchemy 自身管理 posts = db.relationship( 'Post', #用于指定表之间的双向关系,如果在一对多的关系中建立双向的关系,这样的话在对方看来这就是一个多对一的关系 backref='users', #指定 SQLAlchemy 加载关联对象的方式,lazy=subquery: 会在加载 Post 对象后,将与 Post 相关联的对象全部加载 lazy='dynamic') def __init__(self, id, username, password): self.id = id self.username = username self.password = password def __repr__(self): return '<Model User `{}`>'.format(self.username)