Reputation: 2376
I have two Flask-SQLAlchemy models and the respectives Flask-Restless API endpoints as following:
class Person(db.Model):
person_id = db.Column(db.Integer, primary_key=True)
document = db.Column(db.String(), unique=True)
name = db.Column(db.String(45))
class Student(db.Model):
student_id = db.Column(db.Integer, primary_key=True)
person_id = db.Column(db.Integer, db.ForeignKey('person.person_id'))
code = db.Column(db.String(45))
person = db.relationship('Person', backref=db.backref('students', lazy='dynamic'))
manager.create_api(Person, methods=['GET', 'POST'])
manager.create_api(Student, methods=['GET', 'POST'])
These URLs works fine:
http://localhost:8080/api/person
http://localhost:8080/api/student
But when I try to make search queries to related models as described in the official docs, I am getting { "message": "Unable to construct query" }
for these requests:
http://localhost:8080/api/person?q={"filters":[{"name":"students__code","op":"eq","val":"1"}]}
http://localhost:8080/api/person?q={"filters":[{"name":"students","op":"has","val":{"name":"code","op":"eq","val":"1"}}]}
http://localhost:8080/api/student?q={"filters":[{"name":"person__document","op":"eq","val":"111"}]}
http://localhost:8080/api/student?q={"filters":[{"name":"person","op":"any","val":{"name":"document","op":"eq","val":"111"}}]}
I am using Flask 0.10.1, Flask-SQLAlchemy 2.0, Flask-Restless 0.17.0, SQLAlchemy 1.0.6 and Python 2.7.
Upvotes: 1
Views: 645
Reputation: 21
The numerical values in your queries should not be quoted.
Incorrect:
"val":"1"
Correct:
"val":1
Upvotes: 2
Reputation: 9110
Reading from this (see last answer) flask-restless issue:
According to the warning in this section of the SQLAlchemy documentation, dynamic relationship can only be used for one-to-many relationships, not many-to-one relationships. In version 0.8.0b2(+), SQLAlchemy raises an exception if you try to use a dynamic relationship loader for the wrong kind of relationship.
And the warning reads:
Warning
The “dynamic” loader applies to collections only. It is not valid to use “dynamic” loaders with many-to-one, one-to-one, or uselist=False relationships. Newer versions of SQLAlchemy emit warnings or exceptions in these cases.
Update
You cannot have a many-to-one relationship which is dynamic. Your person
relationship is - because of backref
- a many-to-one relationship beside being a one-to-many. From the docs:
To establish a bidirectional relationship in one-to-many, where the “reverse” side is a many to one, specify the backref option.
Upvotes: 1