How does one define a self-referential relationship when using 
declarative objects and reflection?  Based on 
  
http://docs.sqlalchemy.org/en/rel_0_8/orm/relationships.html#adjacency-list-relationships
I wrote the code below but when I run it I get an exception when
the session.query() line is executed:

  [...many frames elided...]
  File "/usr/local/lib/python3.3/site-packages/sqlalchemy/sql/expression.py", 
line 1551, in _only_column_elements
    "'%s'; got: '%s', type %s" % (name, element, type(element)))
  sqlalchemy.exc.ArgumentError: Column-based expression object expected for 
argument 'remote_side'; got: '<built-in function id>', type <class 
'builtin_function_or_method'>

Code:
----------------
import sqlalchemy as sa, sqlalchemy.orm, sqlalchemy.ext, 
sqlalchemy.ext.declarative 
Base = sa.ext.declarative.declarative_base 
(cls=sa.ext.declarative.DeferredReflection)

class Test (Base): 
        __tablename__ = 'test'
        parentobj = sa.orm.relationship ('Test', remote_side=[id])

connectstr = 'postgres://postgres@localhost/test6603976'
engine = sa.create_engine (connectstr)
connection = engine.connect()
meta = sa.MetaData()
meta.reflect (bind=engine)
Base.prepare (engine)
session = sa.orm.sessionmaker (bind=engine)()
session.query(Test).order_by(Test.id).all()
----------------

Database (postgresql):
  CREATE TABLE test (
      id INT PRIMARY KEY,
      parent INT REFERENCES test(id));
  INSERT INTO test VALUES (1, NULL),(2,1);

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to