sqlalchemy

Import data from a joined table as a current (readable) column in SQLAlchemy?


I have this schema:


class Company(db.Model):
    __tablename__  = 'companies'

    id              = db.Column(db.Integer, primary_key=True)

    name            = db.Column(db.String(250), nullable=True, default=None)
    domain          = db.Column(db.String(250), nullable=True, default=None)

    organization_id = db.Column(db.Integer, db.ForeignKey('organizations.id'), nullable=False)


class Contact(db.Model):
    __tablename__  = 'contacts'

    id              = db.Column(db.Integer, primary_key=True)

    name            = db.Column(db.String(250), nullable=True, default=None)
    email           = db.Column(db.String(250), nullable=False)

    company_id      = db.Column(db.Integer, db.ForeignKey('companies.id'), nullable=True, default=None)
    company         = relationship('Company')

    organization_id = db.Column({Import Company.organization_id as eager})

The last line is of course garbage, but it's to show the idea:

I'd like to have the value "organization_id" available in Contact, even though it's not present in the table "contacts", but since it's present in "companies", is there a way to ask SQLAlchemy to load the value from "companies" via a JOIN, and affect it to "contacts" as a read-only value?

That way, when I search for a contact, for instance :

contact = Contact.query.filter(Contact.email = 'test@test.com').first()
print(contact.organization_id) # => 1

Solution

  • You can use the hybrid_property decorator to define an attribute on your class:

    class Contact(db.Model):
    ...
        @hybrid_property
        def organization_id(self):
            return self.company.organization_id if self.company else None
    

    Using contact.organization_id will load the company using the foreign key relationship.