[Sqlalchemy-tickets] Issue #3790: Setting unexpected type for relationship throws exception but mod
Brought to you by:
zzzeek
From: Konsta V. <iss...@bi...> - 2016-09-03 19:13:22
|
New issue 3790: Setting unexpected type for relationship throws exception but modifies session state https://bitbucket.org/zzzeek/sqlalchemy/issues/3790/setting-unexpected-type-for-relationship Konsta Vesterinen: Consider the following model definition and data. ``` #!python import sqlalchemy as sa from sqlalchemy.ext.declarative import declarative_base, synonym_for Base = declarative_base() class Section(Base): __tablename__ = 'section' id = sa.Column(sa.Integer, primary_key=True) name = sa.Column(sa.String) class Question(Base): __tablename__ = 'question' id = sa.Column(sa.Integer, primary_key=True) name = sa.Column(sa.String) section_id = sa.Column(sa.Integer, sa.ForeignKey(Section.id)) section = sa.orm.relationship(Section, backref='questions') engine = sa.create_engine('sqlite:///:memory:') conn = engine.connect() sa.orm.configure_mappers() Base.metadata.create_all(conn) Session = sa.orm.sessionmaker(bind=conn) session = Session() section = Section(questions=[Question(name='1'), Question(name='2')]) session.add(section) session.commit() ``` Now if we did something like this accidentally ``` #!python section.questions[0] = 'Some updated question name' ``` It throws Exception as expected but it also modifies session state so that calling ``` #!python session.commit() ``` will result our given section having only one question. Maybe it would be better if set operations that threw an error would not affect the session state. I encountered this issue while doing stuff in console and later find out that I accidentally deleted one record. |