Tornado之抽屉实战(2)--数据库表设计
经过我们上次分析,数据库要有最基本的四张表,用户表,消息表,类型表,点赞表,评论表,接下来我们看着怎么设计吧
首先我们要清楚,表设计的代码是写在models下的
用户表
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #一张表对应一个类classUserInfo(Base):    #把表名赋给静态字段    __tablename__ ='userinfo'    #序号nid,用户名username,密码password,邮箱email,创建时间ctime    #一行数据就是一个对象    nid =Column(Integer, primary_key=True, autoincrement=True)    username =Column(String(32))    password =Column(String(32))    email =Column(String(32))    ctime =Column(TIMESTAMP)    #建立组合索引,这里是方便在登陆采用不同的登陆方式也能更好的索引数据库    #用户名+密码   和   邮箱+密码   两种组合索引    __table_args__ =(        Index('ix_user_pwd', 'username', 'password'),        Index('ix_email_pwd', 'email', 'password'),    ) | 
信息类型表
| 1 2 3 4 5 6 | classNewsType(Base):    __tablename__ ='newstype'    nid =Column(Integer, primary_key=True, autoincrement=True)    caption =Column(String(32)) | 
信息表
| 1 2 3 4 5 6 7 8 9 10 11 12 | classNews(Base):    __tablename__ ='news'    nid =Column(Integer, primary_key=True, autoincrement=True)    #建立外键---两个    user_info_id =Column(Integer, ForeignKey("userinfo.nid"))    news_type_id =Column(Integer, ForeignKey("newstype.nid"))    ctime =Column(TIMESTAMP)    title =Column(String(32))    url =Column(String(128))    content =Column(String(150)) | 
点赞表
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | classFavor(Base):    #点赞表    __tablename__ ='favor'    nid =Column(Integer, primary_key=True, autoincrement=True)    #点赞者id    user_info_id =Column(Integer, ForeignKey("userinfo.nid"))    #信息id    news_id =Column(Integer, ForeignKey("news.nid"))    ctime =Column(TIMESTAMP)    #建立联合唯一索引    __table_args__ =(        UniqueConstraint('user_info_id', 'news_id', name='uix_uid_nid'),    )  | 
评论表
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | classComment(Base):    __tablename__ ='comment'    nid =Column(Integer, primary_key=True, autoincrement=True)    #评论者id    user_info_id =Column(Integer, ForeignKey("userinfo.nid"))    #评论的信息id    news_id =Column(Integer, ForeignKey("news.nid"))    #如果为None,就是评论文章,如果是数字就是回复某个人    reply_id =Column(Integer, ForeignKey("comment.nid"), nullable=True, default=None)    #顶一下    up =Column(Integer)    #踩一下    down =Column(Integer)    #创建时间    ctime =Column(TIMESTAMP)    #发表设备:手机,电脑,苹果....    device =Column(String(32))    #发表内容    content =Column(String(150)) | 
 
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号