SQLAlchemy relationships on inherited attributes fail

Issue #2723 resolved
Richard Mitchell created an issue

In the given example below, I get the following error while mappers are being configured:

        if nrte.table_name == b.name:
    AttributeError: 'Join' object has no attribute 'name'

This appears to be because the foreign key is a property of the parent model, rather than the target model. Please ignore that this could be avoided by using the id column as foreign key in the example, as this is not possible in my 'real' case.

    class Person(Base):
        __tablename__ = 'people'

        id = Column(Integer, primary_key=True)
        type = Column(Enum('staff', 'student'))
        username = Column(String)

        __mapper_args__ = {
            'polymorphic_on': type
        }

    class Staff(A):
        __tablename__ = 'staff'

        id = Column(Integer, ForeignKey(People.id), primary_key=True)

        __mapper_args__ = {
            'polymorphic_identity': 'staff'
        }

    class Department(Base):
        __tablename__ = 'departments'

        id = Column(Integer, primary_key=True)
        head_username = Column(String, ForeignKey(Staff.username))

        head = relationship(Staff)

This is in SQLAlchemy 0.8.0

Comments (2)

  1. Mike Bayer repo owner

    can you fix up your test please? I'm not sure what "A" is, or "People", I've created a simple test as below and there's no issue:

    from sqlalchemy import *
    from sqlalchemy.orm import *
    from sqlalchemy.ext.declarative import declarative_base
    
    Base = declarative_base()
    
    class Person(Base):
        __tablename__ = 'people'
    
        id = Column(Integer, primary_key=True)
        type = Column(Enum('staff', 'student'))
        username = Column(String)
    
        __mapper_args__ = {
            'polymorphic_on': type
        }
    
    class Staff(Person):
        __tablename__ = 'staff'
    
        id = Column(Integer, ForeignKey(Person.id), primary_key=True)
    
        __mapper_args__ = {
            'polymorphic_identity': 'staff'
        }
    
    class Department(Base):
        __tablename__ = 'departments'
    
        id = Column(Integer, primary_key=True)
        head_username = Column(String, ForeignKey(Staff.username))
    
        head = relationship(Staff)
    
    configure_mappers()
    
  2. Log in to comment