本文介绍了无法为映射表组装任何主键列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试创建数据库架构迁移时,遇到了这个奇怪的错误.您能帮我找出问题所在吗?
When I'm trying to create a database schema migration, I'm getting this weird error. Can you please help me to figure out what's wrong?
$ python app.py db upgrade
[skipped]
sqlalchemy.exc.ArgumentError: Mapper Mapper|EssayStateAssociations|essay_associations could not assemble any primary key columns for mapped table 'essay_associations'
我的模型:
class EssayStateAssociations(db.Model):
__tablename__ = 'essay_associations'
application_essay_id = db.Column(
db.Integer,
db.ForeignKey("application_essay.id"),
primary_key=True),
theme_essay_id = db.Column(
db.Integer,
db.ForeignKey("theme_essay.id"),
primary_key=True),
state = db.Column(db.String, default="pending")
推荐答案
表中不能有两个主键.相反,您必须使用复合主键.可以通过在模型中添加如下所示的PrimaryKeyConstraint
来完成此操作(请记住在关闭__table_args__
中的括号之前添加逗号:
You cannot have two primary keys in a table. Instead, you must use a compound primary key.This can be done by adding a PrimaryKeyConstraint
in your model as below (remember to add a comma before closing the bracket in __table_args__
:
from db import PrimaryKeyConstraint
class EssayStateAssociations(db.Model):
__tablename__ = 'essay_associations'
__table_args__ = (
PrimaryKeyConstraint('application_essay_id', 'theme_essay_id'),
)
application_essay_id = db.Column(
db.Integer,
db.ForeignKey("application_essay.id"))
theme_essay_id = db.Column(
db.Integer,
db.ForeignKey("theme_essay.id"))
state = db.Column(db.String, default="pending")
这篇关于无法为映射表组装任何主键列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!