Hi Community,
I'm getting the (probably trivial) error, but completely clueless about the possible causes. I want to insert two object in the DB using SQLAlchemy. Those objects are related, here are the declarations. Class User:
class User(Base):
__tablename__ = 'cp_user'
id = Column(Integer, Sequence('id_seq'), primary_key=True)
# ... more properties
Class Picture (user may have many of them):
class Picture(Base):
__tablename__ = 'picture'
id = Column(Integer, Sequence('id_seq'), primary_key=True)
authorId = Column('author_id', Integer, ForeignKey('cp_user.id'))
author = relation(User, primaryjoin = authorId == User.id)
# ... more properties
I'm trying to insert the new picture after I've fetched the right user from the DB, or just created it:
s = newSession()
user = s.query(User.name).filter("...some filter here...").first()
if not(user):
user = User()
s.add(user)
s.commit()
picture = Picture()
picture.author = user
s.add(picture)
s.commit()
This fails with the exception: AttributeError: 'RowTuple' object has no attribute '_sa_instance_state'
I tried moving assignment of the author to the constructor -- same error. I can't assign IDs directly -- this breaks the idea of ORM.
What do I do wrong?