From 17ac26272402d9e92dfeb80ad0996eb0e9aff29f Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 13 Jul 2007 08:12:30 +0000 Subject: [PATCH] - a warning is issued by Mapper when two primary key columns of the same name are munged into a single attribute. this happens frequently when mapping to joins (or inheritance). --- CHANGES | 3 +++ lib/sqlalchemy/orm/mapper.py | 4 +++- test/orm/inheritance.py | 4 ++-- test/orm/relationships.py | 14 +++++++------- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/CHANGES b/CHANGES index bd71fe1b1f..94a925cae6 100644 --- a/CHANGES +++ b/CHANGES @@ -25,6 +25,9 @@ - added synchronization to the mapper() construction step, to avoid thread collections when pre-existing mappers are compiling in a different thread [ticket:613] + - a warning is issued by Mapper when two primary key columns of the + same name are munged into a single attribute. this happens frequently + when mapping to joins (or inheritance). - synonym() properties are fully supported by all Query joining/ with_parent operations [ticket:598] - fixed very stupid bug when deleting items with many-to-many diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py index bd5e07b148..375408926f 100644 --- a/lib/sqlalchemy/orm/mapper.py +++ b/lib/sqlalchemy/orm/mapper.py @@ -9,7 +9,7 @@ from sqlalchemy import sql_util as sqlutil from sqlalchemy.orm import util as mapperutil from sqlalchemy.orm import sync from sqlalchemy.orm.interfaces import MapperProperty, MapperOption, OperationContext, SynonymProperty -import weakref +import weakref, warnings __all__ = ['Mapper', 'MapperExtension', 'class_mapper', 'object_mapper', 'EXT_PASS', 'mapper_registry', 'ExtensionOption'] @@ -585,6 +585,8 @@ class Mapper(object): prop = ColumnProperty(deferred=prop.deferred, group=prop.group, *prop.columns) prop.set_parent(self) self.__props[column_key] = prop + if column in self.primary_key and prop.columns[-1] in self.primary_key: + warnings.warn(RuntimeWarning("On mapper %s, primary key column '%s' is being combined with distinct primary key column '%s' in attribute '%s'. Use explicit properties to give each column its own mapped attribute name." % (str(self), str(column), str(prop.columns[-1]), column_key))) prop.columns.append(column) self.__log("appending to existing ColumnProperty %s" % (column_key)) else: diff --git a/test/orm/inheritance.py b/test/orm/inheritance.py index 8d12250887..0458716e5a 100644 --- a/test/orm/inheritance.py +++ b/test/orm/inheritance.py @@ -427,7 +427,7 @@ class InheritTest7(testbase.ORMTest): 'roles' : relation(Role, secondary=user_roles, lazy=False, private=False) } ) - admin_mapper = mapper(Admin, admins, inherits=user_mapper) + admin_mapper = mapper(Admin, admins, inherits=user_mapper, properties={'aid':admins.c.id}) sess = create_session() adminrole = Role('admin') sess.save(adminrole) @@ -463,7 +463,7 @@ class InheritTest7(testbase.ORMTest): } ) - admin_mapper = mapper(Admin, admins, inherits=user_mapper) + admin_mapper = mapper(Admin, admins, inherits=user_mapper, properties={'aid':admins.c.id}) # create roles adminrole = Role('admin') diff --git a/test/orm/relationships.py b/test/orm/relationships.py index 6f652e692e..7c9bbc8987 100644 --- a/test/orm/relationships.py +++ b/test/orm/relationships.py @@ -559,19 +559,19 @@ class TypeMatchTest(testbase.ORMTest): def define_tables(self, metadata): global a, b, c, d a = Table("a", metadata, - Column('id', Integer, primary_key=True), + Column('aid', Integer, primary_key=True), Column('data', String(30))) b = Table("b", metadata, - Column('id', Integer, primary_key=True), - Column("a_id", Integer, ForeignKey("a.id")), + Column('bid', Integer, primary_key=True), + Column("a_id", Integer, ForeignKey("a.aid")), Column('data', String(30))) c = Table("c", metadata, - Column('id', Integer, primary_key=True), - Column("b_id", Integer, ForeignKey("b.id")), + Column('cid', Integer, primary_key=True), + Column("b_id", Integer, ForeignKey("b.bid")), Column('data', String(30))) d = Table("d", metadata, - Column('id', Integer, primary_key=True), - Column("a_id", Integer, ForeignKey("a.id")), + Column('did', Integer, primary_key=True), + Column("a_id", Integer, ForeignKey("a.aid")), Column('data', String(30))) def test_o2m_oncascade(self): class A(object):pass -- 2.47.2