2

I'm trying to create a database to store people and addresses: home and work. I've used inherited classes to create both addresses.

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import ForeignKey, Column, Integer, String, Float
from sqlalchemy.orm import relationship

Base = declarative_base()

class Colleague(Base):
""" Generic Colleague """
    __tablename__ = 'colleagues'
    id = Column(Integer, primary_key=True)
    first_name = Column(String(50))
    last_name = Column(String(50))
    home_address = relationship('Home_Address', back_populates='colleague')
    work_address = relationship('Work_Address', back_populates='colleague')

class Address(Base):
    """ Generic Address """
    __tablename__ = 'addresses'
    id = Column(Integer, primary_key=True)
    adr_type = Column('type', String(50))
    country = Column(String(50))
    state = Column(String(50))
    county = Column(String(50))
    city = Column(String(50))
    district = Column(String(50))
    street = Column(String(50))
    house_number = Column(Integer)
    postal_code = Column(String(50))

    __mapper_args__ = {
        'polymorphic_on' : adr_type
    }

    class Home_Address(Address):
    __tablename__ = 'home_addresses'
    id = Column(Integer, ForeignKey('addresses.id'), primary_key=True)
    colleague_id = Column(Integer, ForeignKey('colleagues.id'))
    colleague = relationship('Colleague', back_populates='home_address')
    __mapper_args__ = {
        'polymorphic_identity' : 'home_addresses'
    }

class Work_Address(Address):
    __tablename__ = 'work_addresses'
    id = Column(Integer, ForeignKey('addresses.id'), primary_key=True)
    colleague_id = Column(Integer, ForeignKey('colleagues.id'))
    colleague = relationship('Colleague', back_populates='work_address')
    __mapper_args__ = {
        'polymorphic_identity' : 'work_addresses'
    }

I've then tried to add an address to a colleague,

adr1 = Home_Address(country='France', city='Paris')
adr2 = Work_Address(country='France', city='Lyon')
col = Colleague(first_name='Joe', last_name='Dalton')
col.home_address = adr1
col.work_address = adr2

but this happens:

TypeError: Incompatible collection type: Home_Address is not list-like

col.home_address gives an empty list, instead of giving me the object Home_Address, how can I set it up to work? Or maybe there is a simpler way of doing it ?

1 Answer 1

1

Colleague -> Address(es) is a one-to-many relationship, right? That would expect a list, so maybe try something like this:

adr1 = Home_Address(country='France', city='Paris')
adr2 = Work_Address(country='France', city='Lyon')
col = Colleague(first_name='Joe', last_name='Dalton')
col.home_address = [adr1]
col.work_address = [adr2]
Sign up to request clarification or add additional context in comments.

2 Comments

That's it, I thought it would need the object, but making it a list works. Thanks !!
You can also add the kwarg uselist=False to the relationship to have SQLA map it to an object instead of a collection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.