On Fri, Sep 15, 2017 at 12:14 PM, Conferency <[email protected]> wrote: > class Paper(db.Model): > __tablename__ = 'papers' > reviewers = db.relationship('User', secondary=paper_reviewer, > backref=db.backref('papers_reviewed', > lazy='dynamic'), > lazy='dynamic') > > paper_reviewer = db.Table('paper_reviewer', > db.Column('user_id', db.Integer, > db.ForeignKey('users.id'), > primary_key=True), > db.Column('paper_id', db.Integer, > db.ForeignKey('papers.id'), > primary_key=True) > ) > > @event.listens_for(Paper.reviewers, 'append') > def paper_reviewers_append(target, value, initiator): > # select queries... > > > > paper.reviewers.append(usr) > db.session.add(paper) > db.session.commit() > > The listener is fired before the execution of paper.reviewers.append(usr). > The execution of append makes two insertions into paper_reviewer table. The > second one causes IntegrityError: UNIQUE constraint failed. > > I tried different operations in the listener. If the operation in listener > triggered any query, the IntegrityError would be caused while the commit() > was executed. Not sure why listener could cause this error. Can anyone give > me some details? Plz
you likely have autoflush occurring within your listener so use "with session.no_autoflush": http://docs.sqlalchemy.org/en/latest/orm/session_api.html?highlight=autocommit#sqlalchemy.orm.session.Session.no_autoflush > > -- > 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. -- 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.
