Hi All

Yes, i know, there is a FAQ-entry and i realized that this issue was
discussed before. Regretably i'm not able to make things work with a
one-to-many relationship using cascade="all, delete-orphan". Now i
discovered that even the example from the documentation raises this
exception (1). I'm using sqlalchemy 0.3.8 and python 2.4.3.

1) 
http://www.sqlalchemy.org/docs/datamapping.html#datamapping_relations_onetomany

Cheers
Lorenz

The whole script for completeness:
from sqlalchemy import *

metadata = MetaData()

# define user table
users_table = Table('users', metadata,
    Column('user_id', Integer, primary_key=True),
    Column('user_name', String(16)),
    Column('password', String(20))
)

# define user address table
addresses_table = Table('addresses', metadata,
    Column('address_id', Integer, primary_key=True),
    Column('user_id', Integer, ForeignKey("users.user_id")),
    Column('street', String(100)),
    Column('city', String(80)),
    Column('state', String(2)),
    Column('zip', String(10))
)

class User(object):
    def __init__(self, user_name, password):
        self.user_name = user_name
        self.password = password

class Address(object):
    def __init__(self, street, city, state, zip):
        self.street = street
        self.city = city
        self.state = state
        self.zip = zip

mapper(Address, addresses_table)
mapper(User, users_table, properties = {
        'addresses' : relation(Address, cascade="all, delete-orphan")
    }
  )

engine = create_engine('sqlite:///mydb.db')
metadata.create_all(engine)

session = create_session(bind_to=engine)

u = User('jane', 'hihilala')
u.addresses.append(Address('123 anywhere street', 'big city', 'UT',
'76543'))
u.addresses.append(Address('1 Park Place', 'some other city', 'OK',
'83923'))

session.save(u)
session.flush()

clear_mappers()  # clear mappers from the previous example
mapper(Address, addresses_table)
mapper(User, users_table, properties = {
        'addresses' : relation(Address, cascade="all, delete-orphan")
    }
  )

del u.addresses[1]
u.addresses.append(Address('27 New Place', 'Houston', 'TX', '34839'))

session.flush()

====>

Traceback (most recent call last):
  File "testappend.py", line 62, in ?
    session.flush()
  File "/opt/local/lib/python2.4/site-packages/sqlalchemy/orm/
session.py", line 302, in flush
    self.uow.flush(self, objects)
  File "/opt/local/lib/python2.4/site-packages/sqlalchemy/orm/
unitofwork.py", line 200, in flush
    flush_context.register_object(obj,
isdelete=object_mapper(obj)._is_orphan(obj))
  File "/opt/local/lib/python2.4/site-packages/sqlalchemy/orm/
mapper.py", line 290, in _is_orphan
    raise exceptions.FlushError("instance %s is an unsaved, pending
instance and is an orphan (is not attached to %s)" %
sqlalchemy.exceptions.FlushError: instance <__main__.Address object at
0x12abf10> is an unsaved, pending instance and is an orphan (is not
attached to any parent 'User' instance via that classes' 'addresses'
attribute)


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