python - Setting Foreign Keys with SQLAlchemy for FastAPI
问题描述
I have the following tables in my models module and I have to set Foreign Key relations between some of their attributes. However, I am failing.
class Exercise(Base):
__tablename__= "Exercise"
id=Column(Integer, primary_key= True)
name=Column(VARCHAR(250))
animation_id=Column(Integer, ForeignKey('Animation.id')) #foreign key -> animation.id
animation=relationship("Animation", back_populates="exercise")
class Animation(Base):
__tablename__="Animation"
id=Column(Integer,primary_key=True,index=True,autoincrement=True)
name=Column(VARCHAR(250))
description=Column(VARCHAR(250))
exercise=relationship("Exercise", back_populates="animation")
Im trying to create a relationship between Exercise.animation_id and Animation.id. I tried adding ForeignKey('Animation.id')) as in the code but it did not work.
I can't think of anything else. Thanks in advance.
解决方案
Fixed the issue by switching to InnoDB from MyISAM. Turns out MyISAM does NOT support foreign keys.
class Exercise(Base):
__tablename__= "Exercise"
__table_args__= {
'mysql_engine':'InnoDB'
}
推荐阅读
- feign - 如何捕捉 Feign OkhhtpClient SocketTimeOut 异常?
- python - 在 matplotlib 中增加 gif 分辨率
- javascript - 如何将一个对象拼接成一个对象数组?
- python - 按组根据其他列更改列值
- javascript - NodeJS - HTML文本内容的每个单词的边界矩形/坐标/偏移(以像素为单位)
- c++ - 类引用成员 - 不是具有合成构造函数的类中的错误,而是用户定义的构造函数中的错误
- javascript - 使用键过滤对象数组
- python - tf.contrib.rnn.LayerNormBasicLSTMCell 的贬低
- javascript - Amcharts 热图工具提示适配器
- arm64 - 如何在 EL2 处处理 arm8 (AArch64) EL1 IRQ/FIQ ISR?