[sqlalchemy] Strange Error: FlushError: Instance Child at 0x1087b30 has a NULL identity key.
Hi everyone, I'm writing a script using sqlalchemy 0.66 and sqlite3. In my script I'm trying to set up a one to one relation between two tables via ORM. Here is a simplified code showing my situation: from sqlalchemy import * from sqlalchemy.orm import * class Parent(object) : def __init__(self, name) : self.name = name class Child(object) : def __init__(self, name) : self.name = name engine = create_engine(sqlite:///test.db, echo=False) metadata = MetaData(engine) prn_tab = Table(parent, metadata, Column('id', Integer, primary_key=True), Column('name', String), sqlite_autoincrement=True) prn_tab.create() chl_tab = Table(child, metadata, Column('id', Integer, primary_key=True), Column('name', String), sqlite_autoincrement=True) chl_tab.create() fck = ForeignKeyConstraint chl_tab.append_constraint(fck([chl_tab.c.id], [prn_tab.c.id])) rel = relationship(Child, uselist=False, passive_deletes=True) mapper(Parent, prn_tab, properties={child:rel}) mapper(Child, chl_tab) Session = sessionmaker() Session.configure(bind=engine) session = Session() session.add(Child(Carl)) session.commit() if you run the above code you will see that the commit statement generates the following error: sqlalchemy.orm.exc.FlushError: Instance Child at 0x1087b30 has a NULL identity key. Check if this flush is occuring at an inappropriate time, such as during a load operation. I really cannot understand the reason why this error is raisedcan anyone give a hint? Tahnks in advance! -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Re: [sqlalchemy] Strange Error: FlushError: Instance Child at 0x1087b30 has a NULL identity key.
SQLAlchemy doesn't consider a primary key column that is also a foreign key to another column to be autogenerating of a new primary key value. So it is not fetched here due to the FK on child.id. If the SQLite database here had foreign key constraints enabled the below operation would fail since there is no parent id of 1 present.SQLite is unique in that such a pattern is even allowed, not to mention you are tricking it by adding the FK constraint *after* the create, so the error message isn't well tailored here, but since there is already ticket 2170 to improve this, will add this contingency to the message. On May 22, 2011, at 1:35 PM, Massi wrote: from sqlalchemy import * from sqlalchemy.orm import * class Parent(object) : def __init__(self, name) : self.name = name class Child(object) : def __init__(self, name) : self.name = name engine = create_engine(sqlite:///test.db, echo=False) metadata = MetaData(engine) prn_tab = Table(parent, metadata, Column('id', Integer, primary_key=True), Column('name', String), sqlite_autoincrement=True) prn_tab.create() chl_tab = Table(child, metadata, Column('id', Integer, primary_key=True), Column('name', String), sqlite_autoincrement=True) chl_tab.create() fck = ForeignKeyConstraint chl_tab.append_constraint(fck([chl_tab.c.id], [prn_tab.c.id])) rel = relationship(Child, uselist=False, passive_deletes=True) mapper(Parent, prn_tab, properties={child:rel}) mapper(Child, chl_tab) Session = sessionmaker() Session.configure(bind=engine) session = Session() session.add(Child(Carl)) session.commit() -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.