From b60185a078ec7143d44f5980043f143d53a9caf3 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 22 Feb 2009 19:35:36 +0000 Subject: [PATCH] - Declarative will accept a table-bound column as a property when used in conjunction with __table__, if the column is already present in __table__. The column will be remapped to the given key the same way as when added to the mapper() properties dict. --- CHANGES | 5 +++++ lib/sqlalchemy/ext/declarative.py | 4 +++- test/ext/declarative.py | 12 ++++++++++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/CHANGES b/CHANGES index c0916e3bb8..ddff28fb7c 100644 --- a/CHANGES +++ b/CHANGES @@ -108,6 +108,11 @@ CHANGES - Declarative will properly interpret the "foreign_keys" argument on a backref() if it's a string. + - Declarative will accept a table-bound column as a property + when used in conjunction with __table__, if the column is already + present in __table__. The column will be remapped to the given + key the same way as when added to the mapper() properties dict. + 0.5.2 ====== diff --git a/lib/sqlalchemy/ext/declarative.py b/lib/sqlalchemy/ext/declarative.py index 79677c0844..084ec2699a 100644 --- a/lib/sqlalchemy/ext/declarative.py +++ b/lib/sqlalchemy/ext/declarative.py @@ -480,7 +480,9 @@ def _as_declarative(cls, classname, dict_): else: table = cls.__table__ if cols: - raise exceptions.ArgumentError("Can't add additional columns when specifying __table__") + for c in cols: + if not table.c.contains_column(c): + raise exceptions.ArgumentError("Can't add additional column %r when specifying __table__" % key) mapper_args = getattr(cls, '__mapper_args__', {}) if 'inherits' not in mapper_args: diff --git a/test/ext/declarative.py b/test/ext/declarative.py index 81009384b9..738f374955 100644 --- a/test/ext/declarative.py +++ b/test/ext/declarative.py @@ -65,12 +65,20 @@ class DeclarativeTest(DeclarativeTestBase): self.assertRaisesMessage(sa.exc.InvalidRequestError, "does not have a __table__", go) def test_cant_add_columns(self): - t = Table('t', Base.metadata, Column('id', Integer, primary_key=True)) + t = Table('t', Base.metadata, Column('id', Integer, primary_key=True), Column('data', String)) def go(): class User(Base): __table__ = t foo = Column(Integer, primary_key=True) - self.assertRaisesMessage(sa.exc.ArgumentError, "add additional columns", go) + # can't specify new columns not already in the table + self.assertRaisesMessage(sa.exc.ArgumentError, "Can't add additional column 'foo' when specifying __table__", go) + + # regular re-mapping works tho + class Bar(Base): + __table__ = t + some_data = t.c.data + + assert class_mapper(Bar).get_property('some_data').columns[0] is t.c.data def test_undefer_column_name(self): # TODO: not sure if there was an explicit -- 2.47.2