On Jun 9, 2010, at 7:10 PM, [email protected] wrote:

> Hello.
> 
> I have 2 tables with one-to-one relation, and I got some unexpected
> behaviour from sqlalchemy.
> In attach there is demonstration script and its log.
> 
> If I try to add object into table "right" for already existing key I
> expect error on DB level, telling about violation of unique
> constraint, but sqlalchemy before try to insert new row, set relation
> key to NULL for already existing object...
> 
> Is it right?
> How can I avoid such behavior?

if you are replacing an object reference with another one, then yes SQLA will 
null out the key for the old one first, since you have removed it from its 
parent by replacing it.  If you dont want the NULL allowed, the usual approach 
is to have the left_idnr column be NOT NULL - the database then does the work 
of disallowing the operation to proceed.   There is a setting for 
passive_deletes, 'all', which disallows the "nulling out" of the foreign key, 
but that only applies to a cascading deletion scenario which is not the case 
here.

For one-to-ones I usually set the foreign key on the child as the primary key 
as well.    You can see me asking about this (since a DBA gave me some 
resistance about it recently) here: 
http://stackoverflow.com/questions/2967450/foreign-key-constraints-on-primary-key-columns-issues
    

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to