我是新开发人员,我尝试在flask中创建小型应用程序-sqlalchemy和flask_admin。 这是我的模特:
class Language(db.Model):
__tablename__ = "languages"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(length=50), nullable=False)
class Article(db.Model):
__tablename__ = 'articles'
from_language_id = db.Column(db.Integer, db.ForeignKey('languages.id'), nullable=True)
to_language_id = db.Column(db.Integer, db.ForeignKey('languages.id'), nullable=True)
language_from = db.relationship('Language', backref='article_language_from')
language_to = db.relationship('Language', backref='article_language_to')
模型让我看到这个错误:
sqlalchemy.exc.AmbiguousForeignKeysError:无法确定联接 父/子表之间的关系条件 article.language_from-有多个外键路径链接 桌子。指定“ foreign_keys”参数,并提供 那些应该算作包含外键的列 引用父表。
我想两次在模型文章中使用模型语言from_language_id和to_language_id, 我该如何解决我的错误?
编辑:我的错误,错误来自flask_admin ==> https://pastebin.com/R2E2zp18,我该如何解决?
答案 0 :(得分:0)
尝试一下:
class Language(db.Model):
__tablename__ = "languages"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(length=50), nullable=False)
class Article(db.Model):
__tablename__ = 'articles'
id = db.Column(db.Integer, primary_key=True)
from_language_id = db.Column(db.Integer, db.ForeignKey('languages.id'), nullable=True)
to_language_id = db.Column(db.Integer, db.ForeignKey('languages.id'), nullable=True)
language_from = db.relationship('Language', backref='article_language_from')
language_to = db.relationship('Language', backref='article_language_to')
输出:
test=# \d articles
Table "public.articles"
Column | Type | Collation | Nullable | Default
------------------+---------+-----------+----------+--------------------------------------
id | integer | | not null | nextval('articles_id_seq'::regclass)
from_language_id | integer | | |
to_language_id | integer | | |
Indexes:
"articles_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
"articles_from_language_id_fkey" FOREIGN KEY (from_language_id) REFERENCES languages(id)
"articles_to_language_id_fkey" FOREIGN KEY (to_language_id) REFERENCES languages(id)
test=# \d languages;
Table "public.languages"
Column | Type | Collation | Nullable | Default
--------+-----------------------+-----------+----------+---------------------------------------
id | integer | | not null | nextval('languages_id_seq'::regclass)
name | character varying(50) | | not null |
Indexes:
"languages_pkey" PRIMARY KEY, btree (id)
Referenced by:
TABLE "articles" CONSTRAINT "articles_from_language_id_fkey" FOREIGN KEY (from_language_id) REFERENCES languages(id)
TABLE "articles" CONSTRAINT "articles_to_language_id_fkey" FOREIGN KEY (to_language_id) REFERENCES languages(id)
答案 1 :(得分:-1)
您好,我只是复制了您的代码并在Article
类中添加了ID,here可以正常工作