sqlalchemy CompileError从m2m表中删除行时未使用的列名

per*_*hon 10 python sqlalchemy flask-sqlalchemy

有一个m2m表连接一个模型的实例,使父子关系.

companies_connections = db.Table(
    'companies_connections',
    db.Column('parent_id', db.BigInteger(), db.ForeignKey('company.id'), primary_key=True),
    db.Column('child_id', db.BigInteger(), db.ForeignKey('company.id'), primary_key=True),
)
Run Code Online (Sandbox Code Playgroud)

尝试从after_insert事件监听器中的表中删除行我只有Connection对象,因为Session正在处理其他flush事件.但是使用

q = companies_connections.delete(
    and_(
        companies_connections.c.parent_id == 10,
        companies_connections.c.child_id == 23
    )
)
connection.execute(q)
Run Code Online (Sandbox Code Playgroud)

我明白了

CompileError: Unconsumed column names: parent_id_1, child_id_1
Run Code Online (Sandbox Code Playgroud)

为什么?

V. *_*ula 3

您应该在where方法内指定条件:

q = companies_connections.delete().where(
   and_(
       companies_connections.c.parent_id == 10,
       companies_connections.c.child_id == 23
   )
)
connection.execute(q)
Run Code Online (Sandbox Code Playgroud)

http://docs.sqlalchemy.org/en/latest/core/tutorial.html#deletes

此外,表应该使用元数据定义:http://docs.sqlalchemy.org/en/latest/core/tutorial.html#define-and-create-tables