sqlalchemy核心中的自引用列

时间:2019-02-06 02:19:16

标签: python sqlalchemy

https://docs.sqlalchemy.org/en/latest/orm/self_referential.html解释了表的列如何引用ORM中的表。列如何在SQLAlchemy Core中引用其包含表?

例如,这将不起作用,因为router.post('/addnewproperty', validationChecks, (req, res, next) => { const errors = validationResult(req); if (!errors.isEmpty()) { console.log('text validation FAILED'); return res.status(400).json({ errors: errors.array() }); } imageUpload(req, res, (multerErr) => { if(multerErr){ console.log('Multer validation FAILED'); return res.status(400).json({ errors: [multerErr.message] }); }else{ console.log('validation PASSED'); } }); }) 在需要引用时尚未定义。

person

1 个答案:

答案 0 :(得分:1)

使用ForeignKey的字符串参数形式,即'tablename.columnkey'

person = sa.Table(
    "person",
    metadata,
    sa.Column("person_id", sa.BigInteger, primary_key=True, autoincrement=True),
    sa.Column("mother", sa.ForeignKey("person.person_id")),  # Note: no `.c.`
)

string参数是惰性处理的,因此只要它们存在于元数据中,它就可以引用其他模块中的示例表而不必导入它们。在这种情况下,它允许引用表本身。