SQLAlchemy Notes

From PeformIQ Upgrade
Jump to navigation Jump to search

Joins

From PIQ_Admin /CRM/Contacts

sql = select([Contact.Contact_ID, Contact.FirstName, Contact.LastName, Contact.Phone, Contact.MobilePhone, Contact.Account_FK, Account.Name])\
          .where(Contact.Account_FK == Account.Account_ID).order_by(Contact.LastName, Contact.FirstName)

ctx.Contacts = db.session.execute(sql)

Failed experiments:

# ctx.Contacts = Contact.query.filter_by(Owner_FK=ctx.User.User_ID).all()

# j = join(Contact, Account, Contact.Account_FK == Account.Account_ID)

# stmt = db.session.execute(select([Contact.Contact_ID, Account.Account_ID])).select_from(j)

filter_by() vs. filter()

Links:

  • a