# may be a join or other construct
self.tables = sqlutil.TableFinder(self.mapped_table)
+ if not len(self.tables):
+ raise exceptions.InvalidRequestError("Could not find any Table objects in mapped table '%s'" % str(self.mapped_table))
+
# determine primary key columns
self.pks_by_table = {}
def get_children(self, column_collections=True, **kwargs):
return (column_collections and list(self.columns) or []) + \
- list(self._froms) + \
+ list(self.locate_all_froms()) + \
[x for x in (self._whereclause, self._having, self._order_by_clause, self._group_by_clause) if x is not None]
def _recorrelate_froms(self, froms):
'orm.lazy_relations',
'orm.eager_relations',
'orm.mapper',
+ 'orm.selectable',
'orm.collection',
'orm.generative',
'orm.lazytest1',
--- /dev/null
+"""all tests involving generic mapping to Select statements"""
+
+import testbase
+from sqlalchemy import *
+from sqlalchemy.orm import *
+from testlib import *
+from fixtures import *
+from query import QueryTest
+
+class SelectableNoFromsTest(ORMTest):
+ def define_tables(self, metadata):
+ global common_table
+ common_table = Table('common', metadata,
+ Column('id', Integer, primary_key=True),
+ Column('data', Integer),
+ Column('extra', String(45)),
+ )
+
+ def test_no_tables(self):
+ class Subset(object):
+ pass
+ selectable = select(["x", "y", "z"]).alias('foo')
+ try:
+ mapper(Subset, selectable)
+ compile_mappers()
+ assert False
+ except exceptions.InvalidRequestError, e:
+ assert str(e) == "Could not find any Table objects in mapped table 'SELECT x, y, z'", str(e)
+
+ def test_basic(self):
+ class Subset(Base):
+ pass
+
+ subset_select = select([common_table.c.id, common_table.c.data]).alias('subset')
+ subset_mapper = mapper(Subset, subset_select)
+
+ sess = create_session(bind=testbase.db)
+ l = Subset()
+ l.data = 1
+ sess.save(l)
+ sess.flush()
+ sess.clear()
+
+ assert [Subset(data=1)] == sess.query(Subset).all()
+
+ # TODO: more tests mapping to selects
+
+if __name__ == '__main__':
+ testbase.main()
\ No newline at end of file
assert str(clause2) == str(t1.join(t2, t1.c.col2==t2.c.col3))
def test_select(self):
- s = t1.select()
- s2 = select([s])
+ s2 = select([t1])
s2_assert = str(s2)
- s3_assert = str(select([t1.select()], t1.c.col2==7))
+ s3_assert = str(select([t1], t1.c.col2==7))
class Vis(ClauseVisitor):
def visit_select(self, select):
select.append_whereclause(t1.c.col2==7)
print "------------------"
- s4_assert = str(select([t1.select()], and_(t1.c.col2==7, t1.c.col3==9)))
+ s4_assert = str(select([t1], and_(t1.c.col2==7, t1.c.col3==9)))
class Vis(ClauseVisitor):
def visit_select(self, select):
select.append_whereclause(t1.c.col3==9)
assert str(s3) == s3_assert
print "------------------"
- s5_assert = str(select([t1.select()], and_(t1.c.col2==7, t1.c.col1==9)))
+ s5_assert = str(select([t1], and_(t1.c.col2==7, t1.c.col1==9)))
class Vis(ClauseVisitor):
def visit_binary(self, binary):
if binary.left is t1.c.col3: