I'm trying to convert some classes from Elixir to declarative and am
running into a problem defining a simple self-referencing definition.
I've searched the archives and have tried several things but seem to
be missing something:
>>> import sqlalchemy
>>> sqlalchemy.__version__
'0.5.2'
>>> from sqlalchemy import *
>>> from sqlalchemy.orm import *
>>> from sqlalchemy.ext.declarative import declarative_base
>>> Base = declarative_base()
>>> class User(Base):
... __tablename__ = "users"
...
... id = Column(Integer, primary_key = True)
... uid = Column(Unicode(15), unique = True)
... modified_by_id = Column(Integer, ForeignKey("users.id"))
... modified_by = relation("User", remote_side = [id])
>>> class Group(Base):
... __tablename__ = "groups"
...
... id = Column(Integer, primary_key = True)
... gid = Column(Unicode(15), unique = True)
... modified_by_id = Column(Integer, ForeignKey("users.id"))
... modified_by = relation("User")
... inactive = Column(Boolean, default=False)
>>> Base.metadata.bind = "sqlite:///:memory:"
>>> #Base.metadata.bind.echo = True
>>> Base.metadata.create_all()
>>> Session = sessionmaker()
>>> s = Session()
Create a user and group:
>>> user = User(uid = u"user")
>>> s.add(user)
>>> group = Group(gid = u"group")
>>> s.add(group)
>>> s.commit()
The Group.modified_by relation works fine:
>>> group.modified_by = user
>>> s.commit()
>>> group = s.query(Group).get(group.id)
>>> print group.modified_by #doctest: +ELLIPSIS
<__main__.User object at ...>
But setting the User.modified_by relation does not work:
>>> user.modified_by = user
>>> s.commit()
>>> user = s.query(User).get(user.id)
>>> print user.modified_by
None
Setting the User.modified_by_id field directly does:
>>> user.modified_by_id = user.id
>>> s.commit()
>>> user = s.query(User).get(user.id)
>>> print user.modified_by #doctest: +ELLIPSIS
<__main__.User object at ...>
What am I doing wrong?
Thanks in advance,
Shawn
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---