0

比如说,

class TeachingTrains(Base):
    __tablename__ = 'teaching_trains'
    train_id = Column(Integer, primary_key=True)
    name = Column(String(40), default='')
    user_id = Column(ForeignKey('user.user_id'), nullable=False, index=True)
    created = Column(DateTime, nullable=False, default=datetime.now)
    update_user_id = Column(ForeignKey('user.user_id'), index=True,
                            default=None)
    updated = Column(DateTime, nullable=False, default=datetime.now)
    deleted = Column(DateTime, default=None)

    update_user = relationship(
        'User', primaryjoin='TeachingTrains.update_user_id == User.user_id')
    user = relationship(
        'User', primaryjoin='TeachingTrains.user_id == User.user_id')

这个model中定义的外键,和

CREATE TABLE `teaching_trains` (
  `train_id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `name` varchar(40) NOT NULL DEFAULT '' COMMENT '名称',
  `user_id` int(11) NOT NULL COMMENT '创建人ID',
  `created` datetime NOT NULL COMMENT '创建时间',
  `update_user_id` int(11) DEFAULT NULL COMMENT '修改人ID',
  `updated` datetime DEFAULT NULL COMMENT '修改时间',
  `deleted` datetime DEFAULT NULL COMMENT '删除时间',
  `correlation_lesson` varchar(40) DEFAULT '[]',
  `example_video` varchar(40) DEFAULT '' COMMENT '练习示范视频',
  PRIMARY KEY (`train_id`),
  KEY `lessons_user_id` (`user_id`),
  KEY `lessons_update_user_id` (`update_user_id`),
  CONSTRAINT `lessons_update_user_id` FOREIGN KEY (`update_user_id`) REFERENCES `user` (`user_id`),
  CONSTRAINT `lessons_user_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=20046 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT COMMENT='练习信息';

这个表中的外键名,有关联吗?
是否是model中只要指定清楚了字段,表中也指定了同样的字段就行了,外键名无所谓?

海神名 148
2017-09-13 提问
1 个回答
0

外键名字随意,字段名对应好就行

撰写答案

推广链接