Difference between revisions of "SQLAlchemy Notes"

From PeformIQ Upgrade
Jump to navigation Jump to search
(Created page with " =Joins= From PIQ_Admin /CRM/Contacts <pre> sql = select([Contact.Contact_ID, Contact.FirstName, Contact.LastName, Contact.Phone, Contact.MobilePhone, Contact.Accou...")
 
 
(2 intermediate revisions by the same user not shown)
Line 5: Line 5:


<pre>
<pre>
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)


        sql = select([Contact.Contact_ID, Contact.FirstName, Contact.LastName, Contact.Phone, Contact.MobilePhone, Contact.Account_FK, Account.Name])\
ctx.Contacts = db.session.execute(sql)
                  .where(Contact.Account_FK == Account.Account_ID).order_by(Contact.LastName, Contact.FirstName)
 
        ctx.Contacts = db.session.execute(sql)
</pre>
</pre>


Line 15: Line 14:


<pre>
<pre>
        # ctx.Contacts = Contact.query.filter_by(Owner_FK=ctx.User.User_ID).all()
# 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)
# j = join(Contact, Account, Contact.Account_FK == Account.Account_ID)


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


Line 33: Line 31:




[[Category::SQLAlchemy]]
[[Category:SQLAlchemy]]
[[Category::Flask]]
[[Category:Flask]]
[[Category:Database]]

Latest revision as of 09:52, 31 May 2017

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