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 raised....can
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.

Reply via email to