sqlalchemy in simple query

Based outside migration framework
1 demo_class = g.db.query (Model) .get (id)

2 equal 等值
article = session.query(model).filter(model.title=="title").first()

select * from model_table where titile == "title"

3 not equal value ranging

article = session.query(model).filter(model.title != "title").first()

4 in not in
articles = session.query(model).filter(model.title.in_(["one", "two"]))

articles = session.query(model).filter(~model.title.in_(["there", "four"])

5 is null
articles = session.query(model).filter(model.count==none).all()
6 is not null
artiles = session.query(model).filter(model.count !=none).all()
7 from sqlalchemy import and_,or_
# and
# articles = session.query(Article).filter(Article.title=='abc',Article.content=='abc').all()
# print(articles)

# or
articles = session.query(Article).filter(or_(Article.title=='abc',Article.content=='abc')).all()
print(articles)

If you want to see the sql statement orm underlying the conversion, you can not to execute any of the methods behind the filter method, we can see the direct printing.

Plainly it is to execute the statement added only filter without actuator

比如:
```python
articles = session.query(Article).filter(or_(Article.title=='abc',Article.content=='abc'))
print(articles)

```

There are configuration items in sqlalchemy the output sql statement directly in the print station

SQLALCHEMY_ECHO = True

 

Using foreign keys

ForeignKey a foreign key relationship between this ratio is relatively simple and clear, and to ensure that the parent table field associated foreign key of the primary key field is consistent but not enough examples ORM

This method does not use long queries more strenuous

class User(Base):
  __tablename__ = 'user'
  id = Column(Integer,primary_key=True,autoincrement=True)
  username = Column(String(50),nullable=False)

class Article(Base):
  __tablename__ = 'article'
  id = Column(Integer,primary_key=True,autoincrement=True)
  title = Column(String(50),nullable=False)
  content = Column(Text,nullable=False)

  uid = Column(Integer,ForeignKey("user.id"))

usre =  session.query(User).filter(User.username =="查询").first()

user = session.query(Article).filter(Article.uid==user.id).all()

 

2 . SQLAlchemy provides a `relationship`, the class attributes may be defined, later when accessing the table directly associated attributes can be accessed through access can be obtained

class Wuye(Base):
  __tablename__ = 'user'
  id = Column(Integer,primary_key=True,autoincrement=True)
  wuye_name = Column(String(50),nullable=False)

  # uptown= relationship("Uptown")

class Uptown(Base):
  __tablename__ = 'article'
  id = Column(Integer,primary_key=True,autoincrement=True)
  title = Column(String(50),nullable=False)
  content = Column(Text,nullable=False)
  wuye_id = Column(Integer,ForeignKey("user.id"))

  Wuye = relationship("Wuye",backref="uptown")

backref reverse attribute is to add a field uptown uptown in the table is a plurality Wuye many relationship

 

Using foreign keys

Get the current query all cells of the small property-many and many-to a query

uptown = session.query (Uptown) .filter (Uptown.wuye_id == current property id) .all ()  

wuye = session.query (Wuye) .filter (Wuye.status == 0) .first ()

uptown = wuye.uptown[0].id

 

Underlying query

pass

Inquire

En join

In this way the number of cells and the presence of link property belonging to the same acquisition

uptown = session.qyery(Uptown).filter(Uptown.wuye_id ==Wuye.id).coun()

 uptown = session(UptownModel).join(WuyeModel, UptownModel.wuye_id==WuyeModel.id).count()

 Links within the default by a foreign key query

# FROM (SELECT uptown.id AS uptown_id, uptown.uptown_name AS uptown_uptown_name
# FROM uptown, wuye
# WHERE uptown.wuye_id = wuye.id) AS anon_

Outer join

outerjoin LEFT OUTER JOIN wuye ON cell to be mainly associated with the query does not exist property is null 

FROM (SELECT uptown.id AS uptown_id, uptown.uptown_name AS uptown_uptown_name
FROM uptown LEFT OUTER JOIN wuye ON uptown.wuye_id = wuye.id) AS anon_1

 

In a subquery select statement, the select statement embedded in another, then the select statement called embedded subquery

subquery () simply understood word query result is the query as a query to another query conditions

Queries district exist property

wuye_id = g.db.query(WuyeModel.id).filter(WuyeModel.status==0).subquery()

uptown = g.db.query(UptownModel).filter(UptownModel.status==0, UptownModel.wuye_id.in_(wuye_id)).all()

Check for phrases

FROM uptown
WHERE uptown.status = %(status_1)s AND uptown.wuye_id IN (SELECT wuye.id
FROM wuye
WHERE wuye.status = %(status_2)s

Guess you like

Origin www.cnblogs.com/wxbn/p/11708017.html