jfinkels / flask-restless

NO LONGER MAINTAINED - A Flask extension for creating simple ReSTful JSON APIs from SQLAlchemy models.
https://flask-restless.readthedocs.io
GNU Affero General Public License v3.0
1.02k stars 301 forks source link

Issues concerning nested resource #661

Closed lennartblom closed 6 years ago

lennartblom commented 7 years ago

Hi there! First of all, thank you for providing such a great tool for creating a sqlalchemy based REST database service so easy!

Unfortunately I have a major issues concerning the methods of nested resource via GET, DELETE and PATCH.

The setup


class User(db.Model, _SleepTrackerEntity):
    """ User """
    id = db.PrimaryKey()
    mobile_devices: BaseQuery = db.one_to_many("MobileDevice", lazy="dynamic")

class MobileDevice(db.Model):
    """ Mobile device """
    id = db.PrimaryKey()
    name = db.Column(db.String(128))
    user = db.many_to_one("User")
    user_id = db.ForeignKeyId("User", schema="public", nullable=False)

Users


--------------------------------
| id | name                                 
--------------------------------
| 1 | Paul 
--------------------------------
| 2 | Sarah
--------------------------------

Mobile Devices


-----------------------------------
| id | name | user_id
-----------------------------------
| 1 | Paul's Dev Samsung | 1
-----------------------------------
| 2 | Paul's Dev Windows phone | 1
-----------------------------------
| 3 | Sarah's Dev Samsung | 2
-----------------------------------

The Issues

GET

When I try to retrieve nested elements I get all devices and get not 404 in case the device does not fit to the user.

GET /api/users/1/mobile_devices/1 <-- Returns valid Mobile Device of Paul GET /api/users/1/mobile_devices/1 <-- Returns valid Mobile Device of Sarah (that's should not be returned)

DELETE

When I try to delete a nested element I get an integrityError with the following details in the application console:

sqlalchemy.exc.IntegrityError: (psycopg2.IntegrityError) null value in column "user_id" violates not-null constraint
DETAIL:  Failing row contains (3, Sarah's Dev Samsung, None).
 [SQL: 'UPDATE public.mobile_device SET user_id=%(user_id)s WHERE public.mobile_device.id = %(public_mobile_device_id)s'] [parameters: {'user_id': None, 'public_mobile_device_id': 18}]
127.0.0.1 - - [15/Aug/2017 10:03:39] "DELETE /api/users/1/mobile_devices/3 HTTP/1.1" 400 -

PATCH

When I do a PATCH method on a nested entity/resource I get the following message, no matter if the nested entity/resource is hold by the identified user or not.

Client

{
    "message": "Model does not have field 'name'"
}

Server

127.0.0.1 - - [15/Aug/2017 10:42:28] "PATCH /api/users/1/mobile_devices/3 HTTP/1.1" 400 -

Any idea what I am doing wrong?

Thank you very much in advance! I really appreciate your work!

Kind regards from Germany, Lennart Blom

jcrben commented 6 years ago

What ended up happening here? Did you resolve your issue?

lennartblom commented 6 years ago

Yes and no... I switched to the 1.0.0b1 version and resolved the issue. Unfortunately a new issue (#668) resulted.