错误在哪里 - 邻接清单?

时间:2016-08-09 03:29:28

标签: python sqlalchemy flask-sqlalchemy

我在尝试自我引用时遇到错误

class Sku(Base, ReprDescrIdMixin):
    __tablename__ = 'SC84'
    id = Column("ID", String, primary_key=True)
    parent_code = Column("PARENTID",String, ForeignKey('sku.id'))
    parent = relationship('Sku', foreign_keys='Sku.parent_code')


sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship Sku.parent - there are no foreign keys linking these tables.  Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.

为什么呢。这是工作http://docs.sqlalchemy.org/en/latest/orm/self_referential.html

3 个答案:

答案 0 :(得分:1)

试试这个

class Sku(Base, ReprDescrIdMixin):
    __tablename__ = 'SC84'
    id = Column("ID", String, primary_key=True)
    parent_code = Column("PARENTID",String, ForeignKey(id))
    parent = relationship('Sku', foreign_keys=parent_code)

使用ForeignKey(...)时,您应该传递参考密钥,有两种方法可以做到这一点

  1. 通过传递一个字符串,其中包含表格名称和与. ForeignKey("table_name.key")连接的键,如ForeignKey(key)
  2. 通过传递像key
  3. 这样的Column对象

    如果ForeignKey(key)已经定义,那么您只能使用方法2,换句话说,如果key = Column(...)位于d3.queue之后

    在我的示例中,我使用方法2,我更喜欢这种方式,以便我的IDE分析代码。

    请参阅此link

答案 1 :(得分:0)

自引用应该是表名(__tablename__)而不是对象名,以下是一对多的关系:

class Sku(Base, ReprDescrIdMixin):
    __tablename__ = 'SC84'
    id = Column("ID", String, primary_key=True)
    parent_code = Column("PARENTID",String, ForeignKey('SC84.id'))
    # ----------------------------------------------->  ^^^^ __tablename__ here
    children = relationship('Sku')

对于MANY-TO-ONE关系:

class Sku(Base, ReprDescrIdMixin):
    __tablename__ = 'SC84'
    id = Column("ID", String, primary_key=True)
    parent_code = Column("PARENTID",String, ForeignKey('SC84.id'))
    parent = relationship('Sku', remote_side=['id'])

答案 2 :(得分:0)

protected function onKeyDown(event:KeyboardEvent):void
{
    if(txtArea.textFlow.flowComposer.numLines > txtArea.heightInLines && event.keyCode == Keyboard.ENTER)
    {
        txtArea.text = txtArea.text.substr(0, txtArea.selectionActivePosition-1) + txtArea.text.substr(txtArea.selectionActivePosition, txtArea.text.length);
    } 
}