Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask-SQLAlchemy join across 3 models and a Table construct

I have 3 models:

class Customer(Model):
    __tablename__ = 'customer'

    id = Column(Integer, primary_key=True)
    statemented_branch_id = Column(Integer, ForeignKey('branch'))
    ...

class Branch(Model):
    __tablename__ = 'branch'

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

class SalesManager(Model):
    __tablename__ = 'sales_manager'

    id = Column(Integer, primary_key=True)
    branches = relationship('Branch', secondary=sales_manager_branches)

And a Table construct:

sales_manager_branches = db.Table(
    'sales_manager_branches',
    Column('branch_id', Integer, ForeignKey('branch.id')),
    Column('sales_manager_id', Integer, ForeignKey('sales_manager.id'))
)

I want to be able to get all Customers for a SalesManager, which means all Customers that have a statemented_branch_id in any of the Branches the SalesManager.branches relationship.

My query is looking something a little like this:

branch_alias = aliased(Branch)
custs = Customer.query.join(branch_alias, SalesManager.branches).\
        filter(Customer.statemented_branch_id == branch_alias.id)

Which is obviously not right.

How can I get all Customers for a SalesManager?

Update

When I try:

Customer.query.\
         join(Branch).\
         join(SalesManager.branches).\
         filter(SalesManager.id == 1).all()

I get an OperationalError:

*** OperationalError: (OperationalError) ambiguous column name: branch.id u'SELECT
customer.id AS customer_id, customer.statemented_branch_id AS 
customer_statemented_branch_id \nFROM customer JOIN branch ON branch.id 
customer.statemented_branch_id, "SalesManager" JOIN sales_manager_branches AS 
sales_manager_branches_1 ON "SalesManager".id = sales_manager_branches_1.sdm_id JOIN 
branch ON branch.id = sales_manager_branches_1.branch_id \nWHERE "SalesManager".id = ?'
(1,)
like image 512
Chris McKinnel Avatar asked Jul 23 '13 04:07

Chris McKinnel


People also ask

How do I join a table in SQLAlchemy ORM?

DEBUG) Base = declarative_base() class table_1(Base): __tablename__ = 'table_1' ID = Column(Integer) FIRST_NAME = Column(String(80),primary_key = True) LAST_NAME = Column(String(80)) class table_2(Base): __tablename__ = 'table_2' ID_1 = Column(Integer) FIRST_NAME_1 = Column(String(80),primary_key = True) LAST_NAME_1 = ...

How do you create a table using SQLAlchemy in Flask?

Step 1 - Install the Flask-SQLAlchemy extension. Step 2 - You need to import the SQLAlchemy class from this module. Step 3 - Now create a Flask application object and set the URI for the database to use. Step 4 - then use the application object as a parameter to create an object of class SQLAlchemy.

How do you create a one to many relationship in Flask-SQLAlchemy?

The comments class attribute defines a One-to-Many relationship between the Post model and the Comment model. You use the db. relationship() method, passing it the name of the comments model ( Comment in this case). You use the backref parameter to add a back reference that behaves like a column to the Comment model.


2 Answers

I needed to add a backref to my SalesManager model that allows SQLAlchemy to figure out how to get from SalesManager to branch.

class SalesManager(Model):
    __tablename__ = 'sales_manager'

    id = Column(Integer, primary_key=True)
    branches = relationship(
        'Branch', secondary=sales_manager_branches, backref="salesmanagers")

And construct the query like this:

Customer.query.\
         join(Branch).\
         join(Branch.salesmanagers).\
         filter(SalesManager.id == 1).all()
like image 137
Chris McKinnel Avatar answered Sep 20 '22 20:09

Chris McKinnel


Try:

SalesManager.query \
            .join(Branch) \
            .join(Customer) \
            .filter(SalesManager.id == 123)

You may need to provide explicit on params, via the second argument to join, or you may need to explicitly add the mapping table in - but in either case what you are trying to do is the following:

SELECT SM.*
FROM sales_manager SM
JOIN sales_manager_branches SMB
    ON SM.id = SMB.sales_manager_id
JOIN branch B
    ON SMB.branch_id = B.id
JOIN customer C
    ON B.id = C.statemented_branch_id
WHERE -- Conditions go here
like image 42
Sean Vieira Avatar answered Sep 18 '22 20:09

Sean Vieira