at least warn, if not error, when a RelationshipProperty is being thrown away

Issue #2674 resolved
Mike Bayer repo owner created an issue
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

user_course = Table('user_course', Base.metadata,
    Column('user_id', Integer, ForeignKey('users.id')),
    Column('course_id', Integer, ForeignKey('courses.id'))
)

user_event = Table('user_event', Base.metadata,
    Column('user_id', Integer, ForeignKey('users.id')),
    Column('event_id', Integer, ForeignKey('events.id'))
)

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)

    courses = relationship("Course", secondary=user_course, backref="users")
    personal_events = relationship("Event", secondary=user_event, backref="users")

class Event(Base):
    __tablename__ = 'events'
    id = Column(Integer, primary_key=True)
    type = Column(String(45))

    __mapper_args__ = {
        'polymorphic_identity': 'event',
        'polymorphic_on': type
    }

class Course(Event):
    __tablename__ = 'courses'
    id = Column(Integer, ForeignKey('events.id'), primary_key=True)

    __mapper_args__ = {
        'polymorphic_identity': 'course',
    }

u1 = User()
c1 = Course()
u1.courses.append(c1)

amazingly, this is very easy to detect and all tests pass except one that is specifically testing if a relationship prop can be replaced (hence a warning):

diff -r b83dd4dc2200bece2896a125be6d4f0911669d15 lib/sqlalchemy/orm/mapper.py
--- a/lib/sqlalchemy/orm/mapper.py  Sat Mar 02 20:27:53 2013 -0500
+++ b/lib/sqlalchemy/orm/mapper.py  Sun Mar 03 12:20:47 2013 -0500
@@ -1117,6 +1117,8 @@
             for col in prop.columns + prop._orig_columns:
                 for col in col.proxy_set:
                     self._columntoproperty[col](col) = prop
+        elif key in self._props:
+            raise sa_exc.InvalidRequestError("Conflicting property %s" % key)

         prop.key = key

Additionally, when this runs we should try to repair the error message in attributes.py to either be more accurate, or more vague if the correct relationship can't be determined.

A warning here can be backported to 0.7.

Comments (2)

  1. Mike Bayer reporter

    75a3f84e5f9c3be645be2ec8906e63b27e9847e5 for 0.8 and a7efed74c3e966fc476b68719ac2ffd8f85a45b9 for 0.7. There's an existing check for "backref conflict" that is expanded in 0.8 to include mapped subclasses and superclasses. There's also a warning for non-column properties being replaced as illustrated above, this also is triggered by the "backref" case in 0.7 where we are sticking with a warning. Finally, the error message for the attribute backref-loop thing has been opened up to refer to all the involved parties, hopefully illustrating that a naming or typing conflict is at the core of the issue.

  2. Log in to comment