我在ORM SQLalchemyst中构建查询时遇到问题
这是我的SQL:
SELECT DISTINCT eps.expedient_id,
eps.first_name,
eps.middle_name,
eps.last_name,
eps.maiden_name,
eps.person_expedient_id,
eps.full_name,
doc_type,
doc_number,
cud.claimer_id
FROM phase_2.expedient_person_search eps
INNER JOIN phase_2.claimer_user_data cud ON eps.expedient_id = cud.expedient_id
WHERE eps.first_name = data.first_name AND doc_type = data.doc_type
ORDER BY order_criteria DESC sorting_order, expedient_id
LIMIT 10 OFFSET 1
我已经尝试过这种方式,但是在运行查询时总是会收到错误消息:
Expedients.query.distinct(Expedients.expedient_id).order_by(Expedients.doc_type.desc()).filter_by(**data).offset(page).limit(perPage).all()
并显示此错误:
Error: (psycopg2.ProgrammingError) SELECT DISTINCT ON expressions must match initial ORDER BY expressions
有人可以告诉我我在做什么错吗?
我的模特:
class Expedients(db.Model):
__tablename__ = "expedient_person_search"
__table_args__ = {'schema':'phase_2'}
expedient_id = db.Column(db.Integer, primary_key=True)
radicate_number = db.Column(db.String(40))
doc_type = db.Column(db.String(10))
doc_number = db.Column(db.String(20),db.ForeignKey('phase_2.claimer_user_data.document_number'))
first_name = db.Column(db.String(128))
middle_name = db.Column(db.String(128))
last_name = db.Column(db.String(128))
maiden_name = db.Column(db.String(128))
person_expedient_id=db.Column(db.Integer)
full_name = db.Column(db.String(128))
编辑22/03/2019
用户模型:
class User(db.Model):
__tablename__ = "claimer_user_data"
__table_args__ = {'schema':'phase_2'}
claimer_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
expedient_id = db.Column(db.Integer)
first_name = db.Column(db.String(128))
last_name = db.Column(db.String(128))
document_type = db.Column(db.String(10))
document_number = db.Column(db.String(20))
username = db.Column(db.String(250))
password_hash = db.Column(db.String(100))
expedients = db.relationship("Expedients")
(具有更多列)
这是mi(** data)数据模型json:
{
"doc_type": "string",
"doc_number": "string",
"first_name": "string",
"middle_name": "string",
"last_name": "string",
"maiden_name": "string",
"radicate_number": "string",
"person_expedient_id": "string",
"page": 0,
"perPage": 0,
"orderCriteria": "string",
"sortingOrder": "string"
}