Hello everybody,
I'm currently using PostgreSQL 8.4.9 and SQLAlchemy 0.7.9, and there's an
issue I just can't seem to solve.
There are 2 tables, User and People. The idea behind this is to have User's
"people_id" column point to that user's personal data, and People's
"user_id" point to the user who created that person. Neither field can be
NULL. This creates an obvious circular dependency when inserting the very
first user and its personal data, hence the use of post_update and use_alter
.
Each schema is in a separate file, so they can't import each other. These
are their structures:
class User(DeclarativeBase):
__tablename__ = 'user'
id = Column(Integer,autoincrement=True,primary_key=True)
people_id = Column(Integer, ForeignKey('people.id', use_alter=True,
name='alter_people_id'), nullable=False, unique=True)
logname = Column(Unicode(200), unique=True)
password = Column(Unicode(128))
people = relation(People, primaryjoin = People.id == people_id,
post_update=True)
----
class People(DeclarativeBase):
__tablename__ = "people"
id = Column(Integer, autoincrement=True, primary_key=True)
name = Column(Unicode(128))
[...]
user_id = Column(Integer, ForeignKey('user.id', use_alter=True,
name='alter_user_id'), nullable=False)
Here's what I'm trying to do:
user = User()
user.logname = 'sysadmin'
user.password = 'blah'
user.id = 1
user.people_id = 1
session.add(user)
ppl = People()
ppl.name = 'sysadmin'
ppl.id = 1
ppl.user_id = 1
session.add(ppl)
session.flush()
transaction.commit()
This raises an IntegrityError (insert or update on table "user" violates
foreign key constraint "alter_people_id"). I tried using
session.add_all([usr,ppl]) instead of session.add, switching the order in
which data is added to the current session, giving the use_alter attirbute
to only one of the tables, and the best I can get is a different
IntegrityError (this time on the other table, violating the other
constraint).
Thanks in advance to whoever can help me.
--
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.