Skip to content

Commit 193aaba

Browse files
committed
updated logic
1 parent cc16ffd commit 193aaba

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

fastapi_jsonapi/data_layers/sqla_orm.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,16 @@ async def apply_relationships(self, obj: TypeModel, data_create: BaseJSONAPIItem
180180
)
181181
else:
182182
assert isinstance(relationship_in, BaseJSONAPIRelationshipDataToOneSchema)
183-
related_data = await self.get_related_object(
184-
related_model=related_model,
185-
related_id_field=relationship_info.id_field_name,
186-
id_value=relationship_in.data.id,
187-
)
188183

184+
if relationship_in.data:
185+
related_data = await self.get_related_object(
186+
related_model=related_model,
187+
related_id_field=relationship_info.id_field_name,
188+
id_value=relationship_in.data.id,
189+
)
190+
else:
191+
setattr(obj, relation_name, None)
192+
continue
189193
try:
190194
hasattr(obj, relation_name)
191195
except MissingGreenlet:

tests/schemas.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,14 @@ class CustomUUIDItemSchema(CustomUUIDItemAttributesSchema):
400400
id: UUID = Field(client_can_set_id=True)
401401

402402

403-
class SelfRelationshipSchema(BaseModel):
403+
class SelfRelationshipAttributesSchema(BaseModel):
404404
name: str
405+
406+
class Config:
407+
orm_mode = True
408+
409+
410+
class SelfRelationshipSchema(SelfRelationshipAttributesSchema):
405411
self_relationship: Optional["SelfRelationshipSchema"] = Field(
406412
relationship=RelationshipInfo(
407413
resource_type="self_relationship",

tests/test_api/test_api_sqla_with_includes.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
CustomUUIDItemAttributesSchema,
5353
PostAttributesBaseSchema,
5454
PostCommentAttributesBaseSchema,
55+
SelfRelationshipAttributesSchema,
5556
SelfRelationshipSchema,
5657
UserAttributesBaseSchema,
5758
UserBioAttributesBaseSchema,
@@ -1845,10 +1846,12 @@ async def test_remove_to_one_relationship_using_by_update(self, async_session: A
18451846
assert child_obj.self_relationship_id == parent_obj.id
18461847

18471848
async with AsyncClient(app=app, base_url="http://test") as client:
1849+
expected_name = fake.name()
18481850
update_body = {
18491851
"data": {
1852+
"id": str(child_obj.id),
18501853
"attributes": {
1851-
"name": fake.name(),
1854+
"name": expected_name,
18521855
},
18531856
"relationships": {
18541857
"self_relationship": {
@@ -1857,9 +1860,23 @@ async def test_remove_to_one_relationship_using_by_update(self, async_session: A
18571860
},
18581861
},
18591862
}
1863+
params = {
1864+
"include": "self_relationship",
1865+
}
18601866
url = app.url_path_for(f"update_{resource_type}_detail", obj_id=child_obj.id)
1861-
res = await client.patch(url, json=update_body)
1867+
res = await client.patch(url, params=params, json=update_body)
18621868
assert res.status_code == status.HTTP_200_OK, res.text
1869+
assert res.json() == {
1870+
"data": {
1871+
"attributes": SelfRelationshipAttributesSchema(name=expected_name).dict(),
1872+
"id": str(child_obj.id),
1873+
"relationships": {"self_relationship": {"data": None}},
1874+
"type": "self_relationship",
1875+
},
1876+
"included": [],
1877+
"jsonapi": {"version": "1.0"},
1878+
"meta": None,
1879+
}
18631880

18641881
await async_session.refresh(child_obj)
18651882
assert child_obj.self_relationship_id is None

0 commit comments

Comments
 (0)