Note that there are some explanatory texts on larger screens.

plurals
  1. POSQLAlchemy: about self referential relation (error: no attribute '_sa_instance_state')
    primarykey
    data
    text
    <p>I want to implement some <code>many to many</code> and <code>self referential</code> mapping kind like the graph of keywords in a <code>keyword net</code>. For example, "apple" is a child of "juicy fruits", at the same time, "apple" is the parent of many "specific species of apples", and "juicy fruits", of courses have many parents like "things eatable". In short a <code>many to many</code> relationship. And, they are both instances of keyword class(<code>self referential</code>). I defined it as following,</p> <pre><code>kw2kw_table=Table('kw2kw_table',Base.metadata, Column('child_id',Integer,ForeignKey('kw_table.id'),primary_key=True), Column('parent_id',Integer,ForeignKey('kw_table.id'),primary_key=True), ) class KW(Base): __tablename__='kw_table' id=Column(Integer,primary_key=True) name=Column(Unicode(28),unique=True) parents=relationship('KW', secondary=kw2kw_table, primaryjoin=id==kw2kw_table.c.child_id, secondaryjoin=id==kw2kw_table.c.parent_id, backref='children', ) def __init__(self,name,parent=None): self.name=name self.children=[] if parent==None: #default to a root keyword self.parents.append(self) else: self.parents.append(parent) </code></pre> <p>in my view file:</p> <pre><code>... keywordName = request.params['keyword'] parentName = request.params.get('parent',u'') if parentName: parent=DBSession.query(KW).filter(KW.name==parentName) if parent: #if parent exists new_kw=KW(keywordName,parent) else: parent=KW(parentName) new_kw=KW(keywordName,parent) else: #if the parent was not provided new_kw=KW(keywordName) DBSession.add(new_kw) ... </code></pre> <p>but, when the view is submitted with<code>(keyword,parent)</code> , I got an error:</p> <pre><code>File "build/bdist.macosx-10.6-intel/egg/sqlalchemy/orm/attributes.py", line 910, in fire_append_event value = fn(state, value, initiator or self) File "build/bdist.macosx-10.6-intel/egg/sqlalchemy/orm/attributes.py", line 1138, in emit_backref_from_collection_append_event child_state, child_dict = instance_state(child), \ AttributeError: 'Query' object has no attribute '_sa_instance_state' </code></pre> <p>I gooogled it, but couldn't figure it out. Please help.</p> <h2>----------update------------------------</h2> <p>details: <a href="https://gist.github.com/actor2019/5417471" rel="nofollow">https://gist.github.com/actor2019/5417471</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload