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 Branch
es 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,)
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 = ...
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.
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.
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()
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With