class User(BaseModel, db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True, autoincrement=True) mobile = db.Column(db.String(20), unique=True) nickname = db.Column(db.String(50), unique=True) _password = db.Column(db.String(20), nullable=False) last_login = db.Column(db.DateTime, default=datetime.now) def __init__(self, mobile, nickname, passwd): self.mobile = mobile self.nickname = nickname self._password = passwd def __repr__(self): return "<User %r>" % self.nickname def check_password(self, password): return True if password == self._password else False def to_dict(self): res_dict = { 'id': self.id, 'nickname': self.nickname, 'mobile': self.mobile, 'last_login': self.last_login } return res_dict
class User(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(200), nullable=False) progress = db.Column(db.Integer) games = db.relationship('Game', secondary="user_game", back_populates='users') password = db.Column(db.String(200), nullable=False)
class BaseModel(object): create_time = db.Column(db.DateTime, default=datetime.now) update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now) @staticmethod def session_commit(): try: db.session.commit() except SQLAlchemyError as err: db.session.rollback() return str(err)
class Game(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(80)) box_art = db.Column(db.String(200)) users = db.relationship('User', secondary="user_game", back_populates="games") def __str__(self): return f'<Game: {self.title}>' def __repr__(self): return f'<Game: {self.title}>'
class User(db.Model): """ 模型,将映射到数据库表中 """ __tablename__ = 'users' # 主键ID id = db.Column(db.INTEGER, primary_key=True, autoincrement=True) nickname = db.Column(db.String(20), nullable=False) mobile = db.Column(db.String(20), nullable=False) password = db.Column(db.String(16), nullable=False) def to_dict(self): res_dict = { 'id': self.id, 'mobile': self.mobile, 'nickname': self.nickname, } return res_dict
class Book(BaseModel, db.Model): __tablename__ = 'books' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(100), nullable=False) category = db.Column(db.String(50)) price = db.Column(db.Float) user_id = db.Column(db.Integer) status = db.Column(db.Enum('0', '1'), default='0') def __init__(self, name, category, price, user_id): self.name = name self.category = category self.price = price self.user_id = user_id def __repr__(self): return "<Book %r>" % self.name def to_dict(self): res_dict = { 'id': self.id, 'name': self.name, 'category': self.category, 'price': self.price, 'user_id': self.user_id, 'status': self.status } return res_dict def delete(self): self.status = "1" return self.session_commit()
class Book(db.Model): """ 模型,将映射到数据库表中 """ __tablename__ = 'books' # 主键ID id = db.Column(db.INTEGER, primary_key=True, autoincrement=True) name = db.Column(db.String(100), nullable=False) category = db.Column(db.String(100), nullable=True) price = db.Column(FLOAT(precision=7, scale=2), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) status = db.Column(db.Enum("0", "1"), default="1") def to_dict(self): res_dict = { 'id': self.id, 'name': self.name, 'category': self.category, 'price': self.price, } return res_dict
class User(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(200), nullable=False) progress = db.Column(db.Integer) games = db.relationship('Game', secondary="user_game", back_populates='users') password = db.Column(db.String(200), nullable=False) class Game(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(80)) box_art = db.Column(db.String(200)) users = db.relationship('User', secondary="user_game", back_populates="games") def __str__(self): return f'<Game: {self.title}>' def __repr__(self): return f'<Game: {self.title}>' user_game_table = db.Table( 'user_game', db.Column('user_id', db.Integer, db.ForeignKey('user.id')), db.Column('game_id', db.Integer, db.ForeignKey('game.id')))