简体   繁体   English

SqlAlchemy - 如何使用模型类而不是物理表名定义外键列

[英]SqlAlchemy - How to define a foreign key column using model class instead of physical table name

In brief简单来说

I want to use model class to define a foreign-key column.我想使用模型类来定义外键列。

My google search on this topic is not helpful so I asked here.我对这个主题的谷歌搜索没有帮助,所以我在这里问。

In full在全

Normally we define column which is a foreign key via physical table name eg guided here通常我们通过物理表名定义作为外键的列,例如在此处引导

author_id = db.Column(db.Integer, db.ForeignKey('author.id'))

The phrase ForeignKey('author.id') helps define column author_id as foreign key column - it refers to talble author where author is the table name.短语ForeignKey('author.id')有助于将列 author_id 定义为外键列 - 它指的是表author ,其中author是表名。

I want to use model class name ie我想使用模型类名,即

author_id = db.Column(db.Integer, db.ForeignKey(Author.id))

But this code gets error但是这段代码出错了

Could not determine join condition between parent/child tables on relationship XX.YYY - there are no foreign keys linking these tables.无法确定关系 XX.YYY 上父/子表之间的连接条件 - 没有链接这些表的外键。 Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.确保引用列与 ForeignKey 或 ForeignKeyConstraint 关联,或指定“primaryjoin”表达式。

How can we get to it?我们怎样才能做到呢?

In brief简单来说

  • From model other than Author来自作者以外的模特
    author_id = db.Column(db.Integer, db.ForeignKey(Author.__table__.c.id))

  • From Author model itself ie self-referential - just list the column name从作者模型本身即自引用 - 只需列出列名
    author_id = db.Column(db.Integer, db.ForeignKey(id))

  • CANNOT use string value不能使用字符串值
    author_id = db.Column(db.Integer, db.ForeignKey('Author.id'))

Full details详细信息

ForeignKey accepts column as first argument which can be of type Column or string in format schema_name.table_name.column_name or table_name.column_name . ForeignKey接受 column 作为第一个参数,它可以是Column或 string 格式的schema_name.table_name.column_nametable_name.column_name Columns that you define in declarative model turn to InstumentedAttribute objects.您在声明性模型中定义的列将变为InstumentedAttribute对象。 That is why db.ForeignKey(Author.id) leads to an error.这就是db.ForeignKey(Author.id)导致错误的原因。 You can access actual column via __table__ attribute of a model:您可以通过模型的__table__属性访问实际列:

author_id = db.Column(db.Integer, db.ForeignKey(Author.__table__.c['id']))

or或者

author_id = db.Column(db.Integer, db.ForeignKey(Author.__table__.c.id))

If you need to define self-referencing foreign key you can simply pass the name of column.如果您需要定义自引用外键,您可以简单地传递列的名称。 While declaration of a model is not finished yet it still has Column type:虽然模型的声明尚未完成,但它仍然具有Column类型:

id = db.Column(db.Integer, primary_key=True)
parent_id = db.Column(db.Integer, db.ForeignKey(id))

Note that you CANNOT define foreign key this way:请注意,您不能以这种方式定义外键:

author_id = db.Column(db.Integer, db.ForeignKey('Author.id'))

You need to specify physical table name, mapping class name won't work for it.您需要指定物理表名,映射类名不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM