SQL Expression Language
=======================
+TypeError: <operator> not supported between instances of 'ColumnProperty' and <something>
+-----------------------------------------------------------------------------------------
+
+This often occurs when attempting to use a :func:`.column_property` or
+:func:`.deferred` object in the context of a SQL expression, usually within
+declarative such as::
+
+ class Bar(Base):
+ __tablename__ = 'bar'
+
+ id = Column(Integer, primary_key=True)
+ cprop = deferred(Column(Integer))
+
+ __table_args__ = (
+ CheckConstraint(cprop > 5),
+ )
+
+Above, the ``cprop`` attribute is used inline before it has been mapped,
+however this ``cprop`` attribute is not a :class:`.Column`,
+it's a :class:`.ColumnProperty`, which is an interim object and therefore
+does not have the full functionality of either the :class:`.Column` object
+or the :class:`.InstrmentedAttribute` object that will be mapped onto the
+``Bar`` class once the declarative process is complete.
+
+While the :class:`.ColumnProperty` does have a ``__clause_element__()`` method,
+which allows it to work in some column-oriented contexts, it can't work in an
+open-ended comparison context as illustrated above, since it has no Python
+``__eq__()`` method that would allow it to interpret the comparison to the
+number "5" as a SQL expression and not a regular Python comparison.
+
+The solution is to access the :class:`.Column` directly using the
+:attr:`.ColumnProperty.expression` attribute::
+
+ class Bar(Base):
+ __tablename__ = 'bar'
+
+ id = Column(Integer, primary_key=True)
+ cprop = deferred(Column(Integer))
+
+ __table_args__ = (
+ CheckConstraint(cprop.expression > 5),
+ )
+
+
.. _error_2afi:
This Compiled object is not bound to any Engine or Connection
import sqlalchemy as sa
from sqlalchemy import testing, util
from sqlalchemy import MetaData, Integer, String, ForeignKey, \
- ForeignKeyConstraint, Index
+ ForeignKeyConstraint, Index, UniqueConstraint, CheckConstraint
from sqlalchemy.testing.schema import Table, Column
from sqlalchemy.orm import relationship, create_session, class_mapper, \
joinedload, configure_mappers, backref, clear_mappers, \
- column_property, composite, Session, properties
+ column_property, composite, Session, properties, deferred
from sqlalchemy.util import with_metaclass
from sqlalchemy.ext.declarative import declared_attr, synonym_for
from sqlalchemy.testing import fixtures, mock
go
)
+ def test_using_explicit_prop_in_schema_objects(self):
+ class Foo(Base):
+ __tablename__ = 'foo'
+
+ id = Column(Integer, primary_key=True)
+ cprop = column_property(Column(Integer))
+
+ __table_args__ = (
+ UniqueConstraint(cprop),
+ )
+ uq = [
+ c for c in Foo.__table__.constraints
+ if isinstance(c, UniqueConstraint)][0]
+ is_(uq.columns.cprop, Foo.__table__.c.cprop)
+
+ class Bar(Base):
+ __tablename__ = 'bar'
+
+ id = Column(Integer, primary_key=True)
+ cprop = deferred(Column(Integer))
+
+ __table_args__ = (
+ CheckConstraint(cprop > sa.func.foo()),
+ )
+ ck = [
+ c for c in Bar.__table__.constraints
+ if isinstance(c, CheckConstraint)][0]
+ is_(ck.columns.cprop, Bar.__table__.c.cprop)
+
+ if testing.requires.python3.enabled:
+ # test the existing failure case in case something changes
+ def go():
+ class Bat(Base):
+ __tablename__ = 'bat'
+
+ id = Column(Integer, primary_key=True)
+ cprop = deferred(Column(Integer))
+
+ # we still can't do an expression like
+ # "cprop > 5" because the column property isn't
+ # a full blown column
+
+ __table_args__ = (
+ CheckConstraint(cprop > 5),
+ )
+ assert_raises(TypeError, go)
+
def test_relationship_level_msg_for_invalid_callable(self):
class A(Base):
__tablename__ = 'a'