On Apr 10, 2007, at 2:12 PM, Frederic Vander Elst wrote:

>
> Hello List,
>
> I have two (related) questions hiding below:
>
> The enclosed code makes mappers with (edit + re-run please)
>
>  . default cascade rules
>  . cascade='all'
>  . cascade='all, delete-orphan'
>
> It tries to test
>
> u = User(); e = Email(); u.append(e) ; sess.fluhs()     # works always
> session.delete(u)                                                   #
> works if any cascade specified
>
> # and *always* deletes Emails
>
> # even *without* delete-orphan

the Email is attached to User.  its not an orphan.  its being deleted  
due to the "delete" cascade which is part of "all".


> Q1: delete-orphan doesn't change the behaviour here.
>
> *but*
>
>  u = User() ; u.name = 'giorgos' ; sess.save(u) ; session.flush() # ok
>  e = Email()
>  e.user_id = u.user_id           # uid = 1

the "user_id" assiginment operation means absolutely nothing to SA's  
ORM:

http://www.sqlalchemy.org/docs/ 
unitofwork.html#unitofwork_api_flush_whatis
http://www.sqlalchemy.org/trac/wiki/ 
FAQ#Isetthefoo_idattributeonmyinstanceto7butthefooattributeisstillNone-s 
houldntithaveloadedFoowithid7

in short, SA ORM leaves the meaning of primary/FK identifiers to the  
database and doesnt keep direct track of their relationships to each  
other in memory - it only tracks actual object references.

>  sess.save(e) ; sess.flush()    # fails with cascade=all, delete-
> orphan

"e" is an orphan, you havent attached it to any parent (the user_id  
operation doesnt count).


>  sess.delete(u)                     # deletes orphans even with only
> cascade='all'
>                                            # tries (and fails, which
> is ok) with no cascade rules (because
>                                            # nullalble = False)
>

this should delete only the user and whatever is immediately attached  
to it (attached as in, an object reference).

> Q2: only cascade = 'all' works.  if we have delete-orphan, we cannot
> create an object
> and forcibly stuff the parent's ID in the relevant column.

nope.

> if we have no cascade, it tries to set email.user_id = Null, which
> fails.

"delete" cascade will delete the email as well, if its literally  
attached to the user object in memory or in the database.  also  
consider putting ON DELETE CASCADE on your tables if you like.



--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to