外键
外键的使用会影响sql语句的传递速度,删除表格的时候也会很麻烦
假如说,我们现在有两个表格
这两个表格有所关联吗?class_id是不是就对应着class_table表格中的id
这就是外键关联,class_id与下面的id相关联,我们可以通过class_id中的数据去查询class_table表中id的数据 (表关联)
外键的创建
- 首先我们需要两张表使用外键进行关联
- 倒入外键的库
from sqlalchemy import foreignKey
- 创建两个表
表1
class User(Base):
__tablename__ = 'user'
id = Column(Integer,primary_key=True,autoincrement=True)
name = Column(String(50))
def __str__(self):
return 'User(title:{},price:{})'.format(self.title,self.price)
表2
class Article(Base):
__tablename__ = 'article'
id = Colum