Some additional info:
Deleting an association instance (KeyphraseAction) directly from the
session works:
kpa = s.query(KeyphraseAction).filter_by(keyphrase_id = kpId).all
()
for item in kpa:
s.delete(item)
s.commit()
On Jun 2, 5:28 pm, hollister <[email protected]> wrote:
> I have a many-to-many schema using an association object and the
> association proxy. I'm able to add data via the ORM, but trying to
> delete from the association (but not delete the left or right tables)
> throws a "AssertionError: Dependency rule tried to blank-out primary
> key column 'keyphrase_action.keyphrase_id' on instance
> '<KeyphraseAction at 0x9da7d8c>'" error. My test code is below. Any
> advice is appreciated!
>
> import os, sys
>
> from sqlalchemy import Column, Integer, String, Table, create_engine,
> schema, types
> from sqlalchemy import orm, MetaData, Column, ForeignKey
> from sqlalchemy.orm import relation, mapper, sessionmaker
> from sqlalchemy.ext.associationproxy import association_proxy
>
> engine = create_engine('sqlite:////home/aw/desktop/test.db',
> echo=True)
>
> meta = MetaData(bind=engine)
>
> # schema
> keyphrase_table = schema.Table('keyphrase', meta.metadata,
> schema.Column('id', types.Integer, primary_key = True,
> autoincrement = True),
> schema.Column('phrase', types.String(160), nullable = False),
> )
>
> action_table = schema.Table('action', meta.metadata,
> schema.Column('id', types.Integer, primary_key = True,
> autoincrement = True),
> schema.Column('action_name', types.Text, nullable = False),
> )
>
> keyphrase_action_table = schema.Table('keyphrase_action',
> meta.metadata,
> schema.Column('keyphrase_id', types.Integer, schema.ForeignKey
> ('keyphrase.id'), primary_key = True),
> schema.Column('action_id', types.Integer, schema.ForeignKey
> ('action.id'), primary_key = True),
> schema.Column('is_deferred', types.Boolean, nullable = False),
> )
>
> meta.create_all()
>
> # classes
> class Keyphrase(object):
> def __init__(self, phrase):
> self.phrase = phrase
>
> # creator function
> def _getKeyphraseAction(d):
> return KeyphraseAction(action = d['action'], isDeferred = d
> ['isDeferred'])
>
> actions = association_proxy('keyphrase_action', 'action', creator
> = _getKeyphraseAction)
>
> class Action(object):
> def __init__(self, name):
> self.action_name = name
>
> class KeyphraseAction(object):
> def __init__(self, keyphrase = None, action = None, isDeferred =
> False):
> self.keyphrase = keyphrase
> self.action = action
> self.is_deferred = isDeferred
>
> # mappers
> mapper(Keyphrase, keyphrase_table)
> mapper(Action, action_table)
>
> mapper(KeyphraseAction, keyphrase_action_table, properties={
> 'keyphrase': relation(Keyphrase,
> backref = 'keyphrase_action'),
> 'action': relation(Action),
> })
>
> # test code
> Session = sessionmaker(bind=engine)
> s = Session()
>
> # add some data
> kp = Keyphrase('fast')
> a = Action('capture_email')
>
> s.add(kp)
> s.add(a)
> s.commit()
>
> # assciate the keyphrase to the action
> kp.actions.append({'action':a,'isDeferred':True})
> s.commit()
>
> #remove the newly created association, leaving the keyphrase and
> actions
> kp = s.query(Keyphrase).get(kp.id)
>
> for i, action in enumerate(kp.actions):
> print action.action_name
> kp.actions.remove(action) # this fails!
>
> s.commit()
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---