[Sqlalchemy-tickets] Issue #3042: ensure warning for implicitly combined columns occurs for inherit
Brought to you by:
zzzeek
|
From: Mike B. <iss...@bi...> - 2014-05-06 00:03:26
|
New issue 3042: ensure warning for implicitly combined columns occurs for inherited classes as well https://bitbucket.org/zzzeek/sqlalchemy/issue/3042/ensure-warning-for-implicitly-combined Mike Bayer: test: ``` #!python from sqlalchemy import * from sqlalchemy.orm import * from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class A(Base): __tablename__ = 'a' id = Column(Integer, primary_key=True) class B(A): __tablename__ = 'b' id = Column(Integer, primary_key=True) a_id = Column(Integer, ForeignKey('a.id')) ``` note that we don't get the "same named columns" warning for A.id and B.id because our test for this is too simplistic, assuming that in an inheritance scenario, a subtable with the same column should implicitly combine with that of a supertable. I'm not sure if I intended this to be this way for all implicitly combined columns that were in an inheritance situation or somehow thought these would always be tables in the sync condition, but these are separate columns and the "same named" error should still apply. We will make it a warning in 0.9 and an error in 1.0 as follows: ``` #!diff diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py index 3e93840..1377945 100644 --- a/lib/sqlalchemy/orm/mapper.py +++ b/lib/sqlalchemy/orm/mapper.py @@ -1604,13 +1604,17 @@ class Mapper(_InspectionAttr): prop = self._props.get(key, None) if isinstance(prop, properties.ColumnProperty): - if prop.parent is self: - raise sa_exc.InvalidRequestError( - "Implicitly combining column %s with column " - "%s under attribute '%s'. Please configure one " - "or more attributes for these same-named columns " - "explicitly." - % (prop.columns[-1], column, key)) + if (prop.columns[0], column) not in self._inherits_equated_pairs and \ + not prop.columns[0].shares_lineage(column): + warn_only = prop.parent is not self + msg = ("Implicitly combining column %s with column " + "%s under attribute '%s'. Please configure one " + "or more attributes for these same-named columns " + "explicitly." % (prop.columns[-1], column, key)) + if warn_only: + util.warn(msg) + else: + raise sa_exc.InvalidRequestError(msg) # existing properties.ColumnProperty from an inheriting # mapper. make a copy and append our column to it ``` this is extracted from #3041. needs tests for this specific case. patch is attached which modifies quite a *lot* of tests which suffer from this warning. |