[Sqlalchemy-tickets] Issue #4142: SQLAlchemy accesses attribute table of custom ColumnElement (zzze
Brought to you by:
zzzeek
From: dima-starosud <iss...@bi...> - 2017-12-06 16:34:18
|
New issue 4142: SQLAlchemy accesses attribute table of custom ColumnElement https://bitbucket.org/zzzeek/sqlalchemy/issues/4142/sqlalchemy-accesses-attribute-table-of dima-starosud: This is using sqlalchemy 1.2.0b3. Please considering following code snippet. It fails with an error (shown in code). However there is a workaround to just add `table = None` to `my_literal`, but I suppose this may lead to other problems. ``` #!python Base = declarative_base() class my_literal(expression.ColumnElement): type = Unicode # table = None # oO @compiles(my_literal) def default_my_literal(element, compiler, **kw): return "'my literal'" class _A(Base): __tablename__ = 'a_table' id = Column(Integer, primary_key=True) ml = column_property(my_literal()) __mapper_args__ = {'polymorphic_on': ml} class _B(Base): __tablename__ = 'b_table' id = Column(Integer, primary_key=True) a_id = Column(Integer, ForeignKey(_A.id)) ml = column_property(my_literal()) __mapper_args__ = {'polymorphic_on': ml} class _C(Base): __tablename__ = 'c_table' id = Column(Integer, primary_key=True) b_id = Column(Integer, ForeignKey(_B.id)) ml = column_property(my_literal()) __mapper_args__ = {'polymorphic_on': ml} class A(_A): __mapper_args__ = {'polymorphic_identity': 'my literal'} class B(_B): __mapper_args__ = {'polymorphic_identity': 'my literal'} class C(_C): __mapper_args__ = {'polymorphic_identity': 'my literal'} B.a = relationship(A, backref='bs') C.b = relationship(B, backref='cs') A.cs = association_proxy('bs', 'cs') Base.metadata.create_all(bind=session.connection()) a = A(bs=[B(cs=[C(), C()]), B(cs=[C(), C()])]) session.add(a) session.flush() session.expire_all() assert a.cs # AttributeError: Neither 'my_literal' object # nor 'Comparator' object has an attribute 'table' ``` |