diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2013-07-02 18:02:20 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2013-07-02 18:02:20 -0400 |
commit | 0e219f2eff8954e20d8e3cdae59431da2676b6f4 (patch) | |
tree | c558aadf71b61e30f7d8a60161594da97127539b | |
parent | faa2b43c6ccaaefd90a37512fed1bb5b75c133b8 (diff) | |
download | sqlalchemy-0e219f2eff8954e20d8e3cdae59431da2676b6f4.tar.gz |
ORM descriptors such as hybrid properties can now be referenced
by name in a string argument used with ``order_by``,
``primaryjoin``, or similar in :func:`.relationship`,
in addition to column-bound attributes. [ticket:2761]
-rw-r--r-- | doc/build/changelog/changelog_08.rst | 9 | ||||
-rw-r--r-- | doc/build/changelog/changelog_09.rst | 9 | ||||
-rw-r--r-- | lib/sqlalchemy/ext/declarative/clsregistry.py | 22 | ||||
-rw-r--r-- | test/ext/declarative/test_basic.py | 39 |
4 files changed, 67 insertions, 12 deletions
diff --git a/doc/build/changelog/changelog_08.rst b/doc/build/changelog/changelog_08.rst index 48f57e8cd..d25cb8d67 100644 --- a/doc/build/changelog/changelog_08.rst +++ b/doc/build/changelog/changelog_08.rst @@ -7,6 +7,15 @@ :version: 0.8.2 .. change:: + :tags: feature, orm, declarative + :tickets: 2761 + + ORM descriptors such as hybrid properties can now be referenced + by name in a string argument used with ``order_by``, + ``primaryjoin``, or similar in :func:`.relationship`, + in addition to column-bound attributes. + + .. change:: :tags: feature, firebird :tickets: 2763 diff --git a/doc/build/changelog/changelog_09.rst b/doc/build/changelog/changelog_09.rst index ce37c688d..d86510e73 100644 --- a/doc/build/changelog/changelog_09.rst +++ b/doc/build/changelog/changelog_09.rst @@ -7,6 +7,15 @@ :version: 0.9.0 .. change:: + :tags: feature, orm, declarative + :tickets: 2761 + + ORM descriptors such as hybrid properties can now be referenced + by name in a string argument used with ``order_by``, + ``primaryjoin``, or similar in :func:`.relationship`, + in addition to column-bound attributes. Also in 0.8.2. + + .. change:: :tags: feature, engine :tickets: 2770 diff --git a/lib/sqlalchemy/ext/declarative/clsregistry.py b/lib/sqlalchemy/ext/declarative/clsregistry.py index 95aba93fa..a669e37f4 100644 --- a/lib/sqlalchemy/ext/declarative/clsregistry.py +++ b/lib/sqlalchemy/ext/declarative/clsregistry.py @@ -12,7 +12,7 @@ This system allows specification of classes and expressions used in from ...orm.properties import ColumnProperty, RelationshipProperty, \ SynonymProperty from ...schema import _get_table_key -from ...orm import class_mapper +from ...orm import class_mapper, interfaces from ... import util from ... import exc import weakref @@ -190,19 +190,21 @@ class _GetColumns(object): def __getattr__(self, key): mp = class_mapper(self.cls, configure=False) if mp: - if not mp.has_property(key): + if key not in mp.all_orm_descriptors: raise exc.InvalidRequestError( "Class %r does not have a mapped column named %r" % (self.cls, key)) - prop = mp.get_property(key) - if isinstance(prop, SynonymProperty): - key = prop.name - elif not isinstance(prop, ColumnProperty): - raise exc.InvalidRequestError( - "Property %r is not an instance of" - " ColumnProperty (i.e. does not correspond" - " directly to a Column)." % key) + desc = mp.all_orm_descriptors[key] + if desc.extension_type is interfaces.NOT_EXTENSION: + prop = desc.property + if isinstance(prop, SynonymProperty): + key = prop.name + elif not isinstance(prop, ColumnProperty): + raise exc.InvalidRequestError( + "Property %r is not an instance of" + " ColumnProperty (i.e. does not correspond" + " directly to a Column)." % key) return getattr(self.cls, key) diff --git a/test/ext/declarative/test_basic.py b/test/ext/declarative/test_basic.py index ad2970b70..540f1623f 100644 --- a/test/ext/declarative/test_basic.py +++ b/test/ext/declarative/test_basic.py @@ -21,7 +21,10 @@ from sqlalchemy.testing.util import gc_collect Base = None -class DeclarativeTestBase(fixtures.TestBase, testing.AssertsExecutionResults): +class DeclarativeTestBase(fixtures.TestBase, + testing.AssertsExecutionResults, + testing.AssertsCompiledSQL): + __dialect__ = 'default' def setup(self): global Base Base = decl.declarative_base(testing.db) @@ -289,7 +292,39 @@ class DeclarativeTest(DeclarativeTestBase): foo.rel = u1 assert foo.rel == u1 - def test_string_dependency_resolution_two(self): + def test_string_dependency_resolution_orm_descriptor(self): + from sqlalchemy.ext.hybrid import hybrid_property + + class User(Base): + __tablename__ = 'user' + id = Column(Integer, primary_key=True) + firstname = Column(String(50)) + lastname = Column(String(50)) + game_id = Column(Integer, ForeignKey('game.id')) + + @hybrid_property + def fullname(self): + return self.firstname + " " + self.lastname + + class Game(Base): + __tablename__ = 'game' + id = Column(Integer, primary_key=True) + name = Column(String(50)) + users = relationship("User", order_by="User.fullname") + + s = Session() + self.assert_compile( + s.query(Game).options(joinedload(Game.users)), + "SELECT game.id AS game_id, game.name AS game_name, " + "user_1.id AS user_1_id, user_1.firstname AS user_1_firstname, " + "user_1.lastname AS user_1_lastname, " + "user_1.game_id AS user_1_game_id " + "FROM game LEFT OUTER JOIN \"user\" AS user_1 ON game.id = " + "user_1.game_id ORDER BY " + "user_1.firstname || :firstname_1 || user_1.lastname" + ) + + def test_string_dependency_resolution_no_table(self): class User(Base, fixtures.ComparableEntity): __tablename__ = 'users' |