On Jun 5, 3:57 pm, Michael Bayer <[EMAIL PROTECTED]> wrote:
> On Jun 5, 2007, at 6:39 AM, lingo wrote:
>
>
>
> > I'm using Sqlalchemy with PostgreSQL in a project.
>
> > Some of the relations (foreign keys) in the database (postgresql) have
> > ON_DELETE=RESTRICT which should prevent parent objects with existing
> > child objects from being deleted (tested, works in pg client).
>
> ON_DELETE=RESTRICT is the virtually the same as the default setting
> of "NO ACTION" in Postgres.  SQLAlchemy's ORM organizes SQL
> operations according to foreign key dependency and no errors should
> occur.

First i stripped the primary joins from the relations (thanks, it
saves me a lot of code/work).

now it looks like:
Parent_table = sa.Table('Parent', metadata, autoload=True)
Child_table = sa.Table('Child', metadata, autoload=True)

orm.mapper(Parent,Parent_table, properties={
    'Children' : relation(Child, cascade="save-update")
    }
)

orm.mapper(Child,Child_table, properties={
    'Parent' : relation(Parent, cascade="save-update")
    }
)

I switched on debugging on sqlalchemy to see what happens when i try
to delete a "parent" object.

The weird thing is (and probably was also the case before) that the
parent gets deleted, and the child isn't. After parent-deletion the
foreign key on the child is set to null (or empty). This is not the
expected behavior when using "on delete=restrict", this is more like
"on delete=set null".

In the debug output i spotted some lines mentioning foreign keys, but
I'm not really sure what to look for.

Any hints?

>
>
> > An example of my current mappings:
> > Child_table = sa.Table('Child', metadata, autoload=True)
> > Parent_table = sa.Table('Parent', metadata, autoload=True)
>
> > orm.mapper(Parent,Parent_table, properties={
> >   'Children' :
> > relation(Child,primaryjoin=Parent_table.c.id==Child_table.c.parent_id,
> > backref=backref("Parent", remote_side=[Parent_table.c.id]))
> >   }
> > )
>
> I notice you are specifying primary join conditions manually.  these
> should pick up automatically based on the foreign keys present in
> child/parent table.  is it possible that the foreign keys are not
> being detected in the autoload=True operation ?  try spelling out the
> tables manually not using autoload=True.

setting autoload=False is not really an option for my project
(although i will try to test it to see if the foreign key constraints
will work that way).


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