class Product(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False, unique=True) date_posted = db.Column(db.DateTime, nullable=False, default=datetime.now) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) def __repr__(self): return f"Product('{self.title}', '{self.date_posted}')"
class User(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), unique=True, index=True) role_id = db.Column(db.Integer, db.ForeignKey('roles.id')) def __repr__(self): return '<User %r>' % self.username
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) body = db.Column(db.String(140)) timestamp = db.Column(db.DateTime, index=True, default=datetime.now) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) def __repr__(self): return '<Post {}>'.format(self.body)
class Employee(db.Model): __tablename__ = 'employees' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.Text, nullable=False) department_id = db.Column(db.Integer, db.ForeignKey('departments.id')) def __init__(self, name): self.name = name
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) content = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) def __repr__(self): return f"Post('{self.title}', '{self.date_posted}')"
class Post(db.Model): # backend for post module id = db.Column(db.Integer, primary_key = True) title = db.Column(db.String(20), nullable = False) time = db.Column(db.DateTime, default = datetime.utcnow(), nullable = False) content = db.Column(db.String(200), nullable = False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable = False) def __repr__(self): return f"Post('{self.title}','{self.time}')"
class Couses(db.Model): __tablename__ = 't_courses' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(255)) abstract = db.Column(db.String(255)) addtime = db.Column(db.DateTime) k_id = db.Column(db.Integer, db.ForeignKey('t_kinds.id')) # cascade='delete' 一对多关系中,父表数据删除,字表也跟着删除 videos = relationship('Videos', backref='courses', cascade='delete')
class Videos(db.Model): __tablename__ = 't_videos' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(255)) time = db.Column(db.Integer) hits = db.Column(db.Integer) addtime = db.Column(db.DateTime) abstract = db.Column(db.String(255)) t_id = db.Column(db.Integer) c_id = db.Column(db.Integer, db.ForeignKey('t_courses.id'))
class User(UserMixin, db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(64), unique=True, index=True) username = db.Column(db.String(64), unique=True, index=True) role_id = db.Column(db.Integer, db.ForeignKey('roles.id')) password_hash = db.Column(db.String(128)) @property def password(self): raise AttributeError('password is not a readable attribute') @password.setter def password(self, password): self.password_hash = generate_password_hash(password) def verify_password(self, password): return check_password_hash(self.password_hash, password) def __repr__(self): return '<User %r>' % self.username
from hello import db from datetime import datetime from werkzeug.security import generate_password_hash, check_password_hash from flask_login import UserMixin from hello import login from hashlib import md5 followers = db.Table( 'followers', db.Column('follower_id', db.Integer, db.ForeignKey('user.id')), db.Column('followed_id', db.Integer, db.ForeignKey('user.id'))) @login.user_loader def load_user(id): return User.query.get(int(id)) class User(UserMixin, db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), index=True, unique=True) email = db.Column(db.String(120), index=True, unique=True) password_hash = db.Column(db.String(128)) posts = db.relationship('Post', backref='author', lazy='dynamic') about_me = db.Column(db.String(140)) last_seen = db.Column(db.DateTime, default=datetime.now) followed = db.relationship('User', secondary='followers', primaryjoin=(followers.c.follower_id == id), secondaryjoin=(followers.c.followed_id == id), backref=db.backref('followers', lazy='dynamic'),