From: Mike Bayer Date: Sat, 27 Mar 2010 03:14:16 +0000 (-0400) Subject: - relationships and columns with foreign keys aren't X-Git-Tag: rel_0_6beta3~11 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b7a2d7de4854c26ea7773b1002852d4245cfcc10;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - relationships and columns with foreign keys aren't allowed on declarative mixins, sorry. [ticket:1751] --- diff --git a/CHANGES b/CHANGES index fb0e67f9a2..7ab07aaff9 100644 --- a/CHANGES +++ b/CHANGES @@ -97,6 +97,9 @@ CHANGES __tablename__, __table_args__, etc. now works if the method references attributes on the ultimate subclass. [ticket:1749] + + - relationships and columns with foreign keys aren't + allowed on declarative mixins, sorry. [ticket:1751] 0.6beta2 ======== diff --git a/lib/sqlalchemy/ext/declarative.py b/lib/sqlalchemy/ext/declarative.py index ef1d3e68c9..1f4658b605 100644 --- a/lib/sqlalchemy/ext/declarative.py +++ b/lib/sqlalchemy/ext/declarative.py @@ -507,7 +507,7 @@ Mapped instances then make usage of from sqlalchemy.schema import Table, Column, MetaData from sqlalchemy.orm import synonym as _orm_synonym, mapper, comparable_property, class_mapper from sqlalchemy.orm.interfaces import MapperProperty -from sqlalchemy.orm.properties import PropertyLoader, ColumnProperty +from sqlalchemy.orm.properties import RelationshipProperty, ColumnProperty from sqlalchemy.orm.util import _is_mapped_class from sqlalchemy import util, exceptions from sqlalchemy.sql import util as sql_util @@ -543,7 +543,16 @@ def _as_declarative(cls, classname, dict_): for name in names: obj = getattr(base,name, None) if isinstance(obj, Column): + if obj.foreign_keys: + raise exceptions.InvalidRequestError( + "Columns with foreign keys to other columns " + "are not allowed on declarative mixins at this time." + ) dict_[name]=column_copies[obj]=obj.copy() + elif isinstance(obj, RelationshipProperty): + raise exceptions.InvalidRequestError( + "relationships are not allowed on " + "declarative mixins at this time.") # doing it this way enables these attributes to be descriptors get_mapper_args = '__mapper_args__' in dict_ @@ -778,7 +787,7 @@ def _deferred_relationship(cls, prop): prop.parent, arg, n.args[0], cls)) return return_cls - if isinstance(prop, PropertyLoader): + if isinstance(prop, RelationshipProperty): for attr in ('argument', 'order_by', 'primaryjoin', 'secondaryjoin', 'secondary', '_foreign_keys', 'remote_side'): v = getattr(prop, attr) diff --git a/test/ext/test_declarative.py b/test/ext/test_declarative.py index bfc3ab074d..01ce7a6357 100644 --- a/test/ext/test_declarative.py +++ b/test/ext/test_declarative.py @@ -1914,6 +1914,24 @@ class DeclarativeMixinTest(DeclarativeTestBase): eq_(obj.name,'testing') eq_(obj.foo(),'bar1') eq_(obj.baz,'fu') + + def test_not_allowed(self): + class MyMixin: + foo = Column(Integer, ForeignKey('bar.id')) + + def go(): + class MyModel(Base, MyMixin): + __tablename__ = 'foo' + + assert_raises(sa.exc.InvalidRequestError, go) + + class MyRelMixin: + foo = relationship("Bar") + def go(): + class MyModel(Base, MyRelMixin): + __tablename__ = 'foo' + assert_raises(sa.exc.InvalidRequestError, go) + def test_table_name_inherited(self):