On Jan 30, 2013, at 5:00 AM, Matteo wrote:

> 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)


both of the foreign key columns are nullable=False.  this creates an impossible 
situation, SQL-wise, as only one row can be added at a time.  The row that goes 
in first necessarily needs to have the FK column be NULL, until the second row 
can go in and the UPDATE statement can set it to something.

>  

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to