On 10/13/2016 11:59 AM, Colton Allen wrote:
Hi all,
I have a primary model with a "before_update" event and a secondary
model with a foreign key to the primary model. When I create a
secondary model, the primary model's "before_update" event is triggered.
I believe this is because the backref is being updated. Can I prevent
this behavior?
class Primary(Model):
id = Column(Integer)
@event.listens_for(Primary, 'before_update')
def example(m, c, t):
print("I was triggered by a mapper update!")
class Secondary(Model):
primary_id = Column(ForeignKey)
primary = orm.relationship('Primary', backref='secondaries')
s = Secondary(primary_id=1)
session.add(s)
session.commit()
that example as written cannot invoke the before_update() event. There
is no reference to the Secondary.primary or Primary.secondaries
attributes in any way and they are not involved in the operation.
Feel free to provide a full MCVE that more accurately shows what you are
doing.
For the case where you append an object to a collection, it is necessary
that the primary key of the collection-holding object is copied into the
foreign key attributes of the related object, which counts as a change
event.
You might try to limit your event handler to look at the attributes
which changed that you might care about, for example:
@event.listens_for(B, 'before_update')
def example(m, c, t):
if inspect(t).attrs.x.history:
print("I was triggered by a mapper update!")
I was triggered by a mapper update!
--
You received this message because you are subscribed to the Google
Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to [email protected]
<mailto:[email protected]>.
To post to this group, send email to [email protected]
<mailto:[email protected]>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable
Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.