On Apr 2, 2008, at 11:19 AM, Jim Carroll wrote:
> I _know_ that the tag with the particular ID is already
> in the tags table.
>
> The first time I had this problem, I had a one-to-
> many between just two tables, and I would get this
> error if I did a session.save(user) on the user side (
> the one side) and then did a session.save(log_entry)
> on the many side. I _really_ want to do the session
> .saves like this sometimes.
if you want to do it that way without telling SA about the collection
relation between the two entries, it has no way to "guess" what the
foreign key id should be - you'd need to populate the foreign key
identifier explicitly on your log entry:
session.save(user)
session.flush()
log_entry.user_id = user.id
session.save(log_entry)
session.flush()
> # add a tag
> t = model.Tag('maplesong')
> self.session.save(t)
> self.session.flush() # successful
>
> # and with that tag, we create a tagging
> tg = model.Tagging(self.jim, self.log, t, datetime.today())
> self.jim.tags.append(tg)
> self.log.tags.append(tg)
>
> self.session.flush() # Integrity Error
>
> At which point I get the Exception: IntegrityError 'Cannot
> add or update a child row a foreign key
> constraint fails. FOREIGN KEY(`tag`) REFERENCES
> `tags`(`id`)
>
> I do not want to add the tagging to the tag... t.taggings.
> add(tg) That doesn't make sense to me.
Adding to a collection is not needed; you can just as easily define
the relation as a many-to-one in the other direction. It makes sense
that if a Tagging object is dependent on a parent Tag, that
constructing a Tagging would like the Tag be attached to it. Heres
a bidirectional version (not sure if I'm getting your actual classes
right, but this is the idea):
mapper(model.Tag, self.tags_table, properties={
'taggings':relation(Tagging, backref='parent_tag')
})
The many-to-one side is Tagging.parent_tag. So you could configure
Taggings constructor such that you can say
Tagging(parent_tag=sometag). Same idea, unidirectional:
mapper(model.Tagging, tagging_table, properties={
'parent_tag':relation(Tag)
})
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---