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.

-- 
Fayaz Yusuf Khan
Cloud developer and designer
Dexetra SS, Kochi, India
fayaz.yusuf.khan_AT_gmail_DOT_com
fayaz_AT_dexetra_DOT_com
+91-9746-830-823

Attachment: signature.asc
Description: This is a digitally signed message part.

Reply via email to