On Jun 22, 2011, at 2:53 AM, Fayaz Yusuf Khan wrote:

> On Monday, June 20, 2011 07:34:42 PM Michael Bayer wrote:
>> SQLA doesn't automatically "create" any objects so you'd need to create the
>> Parent object yourself, but you'd also use relationship():
>> 
>> class Child(Base):
>>    parent_name = Column(String, ForeignKey('parent.name'))
>>    parent = relationship(Parent)
>> 
>>    def __init__(self, name):
>>        self.parent = Parent(name)
>> 
>> 
>> relationship() would handle the "parent_name" assignment as well as adding
>> Parent to the Session.
> 
> I did that. But later on, when I had to insert a Child to an existing Parent, 
> it raised IntegrityErrors once again.
> With some IRC help, I found a solution using the UniqueObject recipe 
> (http://www.sqlalchemy.org/trac/wiki/UsageRecipes/UniqueObject). I was about 
> to use that in my code, but then I saw that there's a method called 
> session.merge(), which apears to do the same thing.
> 
> Please correct me if I'm wrong.

The unique object recipe and merge() both have in common that they perform a 
SELECT, then an INSERT if row not found, though the unique object recipe is 
used via a creational pattern, and also supports identification of the object 
by any criterion, not just the primary key.   merge() is used via a 
transference of state pattern and only operates on primary key.

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