class User(UserMixin, db.Model): __tablename__ = "users" id = db.Column(db.Integer, primary_key=True) fullname = db.Column(db.String(255), nullable=False) username = db.Column(db.String(255), nullable=False) #nullable=空白 password = db.Column(db.String(255), nullable=False) # sessionのところ posts = db.relationship('Post', backref='author', lazy=True) #profileの created_at = db.Column(db.DateTime, nullable=False, default=datetime.now) updated_at = db.Column(db.DateTime, nullable=False, default=datetime.now, onupdate=datetime.now) def __init__(self, fullname, username, password): self.fullname = fullname self.username = username self.password = password def is_active(self): return True def is_authenticated(self): return True
class User(db.Model, UserMixin ): # this UserMixin is for our login to keep the session data __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False) password = db.Column(db.String(100), nullable=False) email = db.Column(db.String(200), nullable=True) bio = db.Column(db.String(280), nullable=True) create_at = db.Column(db.DateTime, nullable=False, default=datetime.now) update_at = db.Column(db.DateTime, nullable=False, default=datetime.now, onupdate=datetime.now) posts = db.relationship(('Posts'), backref="editor_posts", lazy=True) comments = db.relationship(('Comments'), backref="editor_comments", lazy=True) friendship = db.relationship(('Friendships'), backref="friendships", lazy=True) friends = db.relationship( 'User', secondary=followtable, primaryjoin=(followtable.c.source_user_id == id), secondaryjoin=(followtable.c.target_user_id == id), backref=db.backref("users", lazy="dynamic"), lazy="dynamic") # primaryjoin=id==friendship.c.source_user_id, # secondaryjoin=id==friendship.c.target_user_id) # select * from user join followtable on user.id = followtable.source_user_id def follow(self, friend): # friend, the argument is User instance if friend not in self.friends: self.friends.append(friend) # friend.friends.append(self) def un_follow(self, friend): if friend in self.friends: self.friends.remove(friend)
class Post(db.Model): __tablename__ = "post" id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) comments = db.relationship('Comment', backref="post", lazy=True) post = db.Column(db.String(1000), nullable=False) created_at = db.Column(db.DateTime, nullable=False, default=datetime.now) updated_at = db.Column(db.DateTime, nullable=False, default=datetime.now, onupdate=datetime.now)
class User(UserMixin, db.Model): __tablename__ = "users" id = db.Column(db.Integer, primary_key=True) fullname = db.Column(db.String(255), nullable=False) username = db.Column(db.String(255), nullable=False) password = db.Column(db.String(255), nullable=False) posts = db.relationship('Post', backref="author", lazy=True) created_at = db.Column(db.DateTime, nullable=False, default=datetime.now) updated_at = db.Column(db.DateTime, nullable=False, default=datetime.now, onupdate=datetime.now)
class Posts(db.Model): __tablename__ = 'posts' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) title = db.Column(db.String(30), nullable=False) content = db.Column(db.String(280), nullable=False) create_at = db.Column(db.DateTime, nullable=False, default=datetime.now) update_at = db.Column(db.DateTime, nullable=False, default=datetime.now, onupdate=datetime.now) comments = db.relationship(('Comments'), backref="author", lazy=True)