Hi, I'm trying to set up a model where child objects are allowed to
not have parents. At present, I can't get SA to leave the children
intact, despite having ondelete="SET NULL" and no "delete-orphan"s.

This is with SA 0.4.3.

To demonstrate my confusion, can someone explain why this code deletes
all my hats:

import sys, time
from datetime import datetime
from sqlalchemy import Table, Column, ForeignKey, MetaData,
create_engine
from sqlalchemy.orm import relation, sessionmaker, mapper, backref
from sqlalchemy import String, Unicode, Integer, DateTime

metadata=MetaData()
engine = create_engine("sqlite:///:memory:")

users_table = Table('tg_user', metadata,
    Column('user_id', Integer, primary_key=True),
)

hat_table = Table('hat', metadata,
    Column('id', Integer, primary_key=True),
    Column('user_id', Integer, ForeignKey('tg_user.user_id',
ondelete='SET NULL')),
)

metadata.create_all(engine)

class User(object):
    pass

class Hat(object):
    pass

mapper(User, users_table)

mapper(Hat, hat_table,
    properties = {
        'user': relation(User, backref=backref("hats",
cascade="all")),
    }
)

Session = sessionmaker(bind=engine, autoflush=False,
transactional=True)
session = Session()

me = User()
me.hats.extend([Hat(), Hat(), Hat()])
session.save(me)
session.flush()

print session.query(Hat).count(), "hats"
session.delete(me)
session.flush()
print session.query(Hat).count(), "hats"

Thank you!
James
--~--~---------~--~----~------------~-------~--~----~
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