from sqlalchemy import create_engine
from sqlalchemy import orm
from sqlalchemy import schema
from sqlalchemy import types
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('sqlite:///')
metadata = schema.MetaData()
BaseObject = declarative_base(metadata=metadata)
Session = orm.sessionmaker(bind=engine)
class Specialism(BaseObject):
__tablename__ = 'specialism'
account_id = schema.Column(types.Integer(),
schema.ForeignKey('account.id',
onupdate='CASCADE', ondelete='CASCADE'),
primary_key=True)
type = schema.Column(types.String(16), primary_key=True)
def __init__(self, type):
self.type = type
class Account(BaseObject):
__tablename__ = 'account'
id = schema.Column(types.Integer(),
schema.Sequence('account_id_seq', optional=True),
primary_key=True, autoincrement=True)
_specialism = orm.relationship(Specialism,
cascade='delete, delete-orphan')
#: List of :term:`specialisms<article specialism>` this account is
#: authorized for.
specialisms = association_proxy('_specialism', 'type')
metadata.create_all(engine)
session = Session()
account = Account()
session.add(account)
account.specialisms = ['foo', 'bar']
assert session.query(Specialism).count() == 2
session.flush()
account.specialisms = ['buz']
session.flush()
assert session.query(Specialism).count() == 1
--
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.
I am fighting a bit of an odd issue (using SQLAlchemy 0.7.3). I have an
account class, where each account has a list of strings managed via an
association proxy. For a reason I can't seem to find the values are not
persisted. The test case below demonstrates this: SQLAlchemy prints a
"Object of type <Specialism> not in session, delete operation along
'Account._specialism' will not proceed" error and the first assert
fails. Strangely enough if I remove the cascade on the _specialism
relationship adding items does work correctly, but trying to remove them
fails with a "Dependency rule tried to blank-out primary key column
'specialism.account_id'" error (which is expected).
- [sqlalchemy] association proxy not persisting? Wichert Akkerman
- Re: [sqlalchemy] association proxy not persisting? Michael Bayer
