Skip to content

Commit

Permalink
Fix query error when model with o2o pk and m2m field (#1783)
Browse files Browse the repository at this point in the history
* fix: query error when m2m combine with o2o

* refactor: more readable
  • Loading branch information
waketzheng authored Nov 25, 2024
1 parent 1e6089d commit 3d18d03
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 27 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ Changelog
0.22
====

0.22.1
------
Fixed
^^^^^
- Fix unable to use ManyToManyField if OneToOneField passed as Primary Key (#1783)

0.22.0
------
Fixed
Expand Down
149 changes: 129 additions & 20 deletions tests/contrib/test_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,42 @@ def test_event_schema(self):
self.Event_Pydantic.model_json_schema(),
{
"$defs": {
"Address_coqnj7_leaf": {
"Address_e4rhju_leaf": {
"additionalProperties": False,
"properties": {
"city": {"maxLength": 64, "title": "City", "type": "string"},
"street": {"maxLength": 128, "title": "Street", "type": "string"},
"m2mwitho2opks": {
"items": {"$ref": "#/$defs/M2mWithO2oPk_leajz6_leaf"},
"title": "M2Mwitho2Opks",
"type": "array",
},
"event_id": {
"maximum": 9223372036854775807,
"minimum": -9223372036854775808,
"title": "Event Id",
"type": "integer",
},
},
"required": ["city", "street", "event_id"],
"required": ["city", "street", "event_id", "m2mwitho2opks"],
"title": "Address",
"type": "object",
},
"M2mWithO2oPk_leajz6_leaf": {
"additionalProperties": False,
"properties": {
"id": {
"maximum": 2147483647,
"minimum": -2147483648,
"title": "Id",
"type": "integer",
},
"name": {"maxLength": 64, "title": "Name", "type": "string"},
},
"required": ["id", "name"],
"title": "M2mWithO2oPk",
"type": "object",
},
"Reporter_fgnv33_leaf": {
"additionalProperties": False,
"description": "Whom is assigned as the reporter",
Expand Down Expand Up @@ -202,7 +222,7 @@ def test_event_schema(self):
},
"address": {
"anyOf": [
{"$ref": "#/$defs/Address_coqnj7_leaf"},
{"$ref": "#/$defs/Address_e4rhju_leaf"},
{"type": "null"},
],
"nullable": True,
Expand All @@ -229,7 +249,7 @@ def test_eventlist_schema(self):
self.Event_Pydantic_List.model_json_schema(),
{
"$defs": {
"Event_padfez": {
"Event_mfxmwb": {
"additionalProperties": False,
"description": "Events on the calendar",
"properties": {
Expand Down Expand Up @@ -282,7 +302,7 @@ def test_eventlist_schema(self):
},
"address": {
"anyOf": [
{"$ref": "#/$defs/Address_coqnj7_leaf"},
{"$ref": "#/$defs/Address_e4rhju_leaf"},
{"type": "null"},
],
"nullable": True,
Expand All @@ -302,22 +322,42 @@ def test_eventlist_schema(self):
"title": "Event",
"type": "object",
},
"Address_coqnj7_leaf": {
"Address_e4rhju_leaf": {
"additionalProperties": False,
"properties": {
"city": {"maxLength": 64, "title": "City", "type": "string"},
"street": {"maxLength": 128, "title": "Street", "type": "string"},
"m2mwitho2opks": {
"items": {"$ref": "#/$defs/M2mWithO2oPk_leajz6_leaf"},
"title": "M2Mwitho2Opks",
"type": "array",
},
"event_id": {
"maximum": 9223372036854775807,
"minimum": -9223372036854775808,
"title": "Event Id",
"type": "integer",
},
},
"required": ["city", "street", "event_id"],
"required": ["city", "street", "event_id", "m2mwitho2opks"],
"title": "Address",
"type": "object",
},
"M2mWithO2oPk_leajz6_leaf": {
"additionalProperties": False,
"properties": {
"id": {
"maximum": 2147483647,
"minimum": -2147483648,
"title": "Id",
"type": "integer",
},
"name": {"maxLength": 64, "title": "Name", "type": "string"},
},
"required": ["id", "name"],
"title": "M2mWithO2oPk",
"type": "object",
},
"Reporter_fgnv33_leaf": {
"additionalProperties": False,
"description": "Whom is assigned as the reporter",
Expand Down Expand Up @@ -392,7 +432,7 @@ def test_eventlist_schema(self):
},
},
"description": "Events on the calendar",
"items": {"$ref": "#/$defs/Event_padfez"},
"items": {"$ref": "#/$defs/Event_mfxmwb"},
"title": "Event_list",
"type": "array",
},
Expand Down Expand Up @@ -467,6 +507,21 @@ def test_address_schema(self):
"title": "Event",
"type": "object",
},
"M2mWithO2oPk_leajz6_leaf": {
"additionalProperties": False,
"properties": {
"id": {
"maximum": 2147483647,
"minimum": -2147483648,
"title": "Id",
"type": "integer",
},
"name": {"maxLength": 64, "title": "Name", "type": "string"},
},
"required": ["id", "name"],
"title": "M2mWithO2oPk",
"type": "object",
},
"Reporter_fgnv33_leaf": {
"additionalProperties": False,
"description": "Whom is assigned as the reporter",
Expand Down Expand Up @@ -544,6 +599,11 @@ def test_address_schema(self):
"properties": {
"city": {"maxLength": 64, "title": "City", "type": "string"},
"street": {"maxLength": 128, "title": "Street", "type": "string"},
"m2mwitho2opks": {
"items": {"$ref": "#/$defs/M2mWithO2oPk_leajz6_leaf"},
"title": "M2Mwitho2Opks",
"type": "array",
},
"event": {"$ref": "#/$defs/Event_zvunzw_leaf"},
"event_id": {
"maximum": 9223372036854775807,
Expand All @@ -552,7 +612,7 @@ def test_address_schema(self):
"type": "integer",
},
},
"required": ["city", "street", "event", "event_id"],
"required": ["city", "street", "event", "event_id", "m2mwitho2opks"],
"title": "Address",
"type": "object",
},
Expand All @@ -563,7 +623,7 @@ def test_tournament_schema(self):
self.Tournament_Pydantic.model_json_schema(),
{
"$defs": {
"Event_jgrv4c_leaf": {
"Event_ln6p2q_leaf": {
"additionalProperties": False,
"description": "Events on the calendar",
"properties": {
Expand Down Expand Up @@ -612,7 +672,7 @@ def test_tournament_schema(self):
},
"address": {
"anyOf": [
{"$ref": "#/$defs/Address_coqnj7_leaf"},
{"$ref": "#/$defs/Address_e4rhju_leaf"},
{"type": "null"},
],
"nullable": True,
Expand All @@ -631,22 +691,42 @@ def test_tournament_schema(self):
"title": "Event",
"type": "object",
},
"Address_coqnj7_leaf": {
"Address_e4rhju_leaf": {
"additionalProperties": False,
"properties": {
"city": {"maxLength": 64, "title": "City", "type": "string"},
"street": {"maxLength": 128, "title": "Street", "type": "string"},
"m2mwitho2opks": {
"items": {"$ref": "#/$defs/M2mWithO2oPk_leajz6_leaf"},
"title": "M2Mwitho2Opks",
"type": "array",
},
"event_id": {
"maximum": 9223372036854775807,
"minimum": -9223372036854775808,
"title": "Event Id",
"type": "integer",
},
},
"required": ["city", "street", "event_id"],
"required": ["city", "street", "event_id", "m2mwitho2opks"],
"title": "Address",
"type": "object",
},
"M2mWithO2oPk_leajz6_leaf": {
"additionalProperties": False,
"properties": {
"id": {
"maximum": 2147483647,
"minimum": -2147483648,
"title": "Id",
"type": "integer",
},
"name": {"maxLength": 64, "title": "Name", "type": "string"},
},
"required": ["id", "name"],
"title": "M2mWithO2oPk",
"type": "object",
},
"Reporter_fgnv33_leaf": {
"additionalProperties": False,
"description": "Whom is assigned as the reporter",
Expand Down Expand Up @@ -711,7 +791,7 @@ def test_tournament_schema(self):
},
"events": {
"description": "What tournaments is a happenin'",
"items": {"$ref": "#/$defs/Event_jgrv4c_leaf"},
"items": {"$ref": "#/$defs/Event_ln6p2q_leaf"},
"title": "Events",
"type": "array",
},
Expand All @@ -727,7 +807,7 @@ def test_team_schema(self):
self.Team_Pydantic.model_json_schema(),
{
"$defs": {
"Event_n2kadx_leaf": {
"Event_lfs4vy_leaf": {
"additionalProperties": False,
"description": "Events on the calendar",
"properties": {
Expand Down Expand Up @@ -775,7 +855,7 @@ def test_team_schema(self):
},
"address": {
"anyOf": [
{"$ref": "#/$defs/Address_coqnj7_leaf"},
{"$ref": "#/$defs/Address_e4rhju_leaf"},
{"type": "null"},
],
"nullable": True,
Expand All @@ -794,22 +874,42 @@ def test_team_schema(self):
"title": "Event",
"type": "object",
},
"Address_coqnj7_leaf": {
"Address_e4rhju_leaf": {
"additionalProperties": False,
"properties": {
"city": {"maxLength": 64, "title": "City", "type": "string"},
"street": {"maxLength": 128, "title": "Street", "type": "string"},
"m2mwitho2opks": {
"items": {"$ref": "#/$defs/M2mWithO2oPk_leajz6_leaf"},
"title": "M2Mwitho2Opks",
"type": "array",
},
"event_id": {
"maximum": 9223372036854775807,
"minimum": -9223372036854775808,
"title": "Event Id",
"type": "integer",
},
},
"required": ["city", "street", "event_id"],
"required": ["city", "street", "event_id", "m2mwitho2opks"],
"title": "Address",
"type": "object",
},
"M2mWithO2oPk_leajz6_leaf": {
"additionalProperties": False,
"properties": {
"id": {
"maximum": 2147483647,
"minimum": -2147483648,
"title": "Id",
"type": "integer",
},
"name": {"maxLength": 64, "title": "Name", "type": "string"},
},
"required": ["id", "name"],
"title": "M2mWithO2oPk",
"type": "object",
},
"Reporter_fgnv33_leaf": {
"additionalProperties": False,
"description": "Whom is assigned as the reporter",
Expand Down Expand Up @@ -874,7 +974,7 @@ def test_team_schema(self):
"title": "Alias",
},
"events": {
"items": {"$ref": "#/$defs/Event_n2kadx_leaf"},
"items": {"$ref": "#/$defs/Event_lfs4vy_leaf"},
"title": "Events",
"type": "array",
},
Expand Down Expand Up @@ -918,6 +1018,7 @@ async def test_eventlist(self):
"address": {
"event_id": self.address.pk,
"city": "Santa Monica",
"m2mwitho2opks": [],
"street": "Ocean",
},
},
Expand Down Expand Up @@ -970,7 +1071,12 @@ async def test_event(self):
{"id": self.team1.id, "name": "Onesies", "alias": None},
{"id": self.team2.id, "name": "T-Shirts", "alias": None},
],
"address": {"event_id": self.address.pk, "city": "Santa Monica", "street": "Ocean"},
"address": {
"event_id": self.address.pk,
"city": "Santa Monica",
"m2mwitho2opks": [],
"street": "Ocean",
},
},
)

Expand Down Expand Up @@ -1004,6 +1110,7 @@ async def test_address(self):
"alias": None,
},
"event_id": self.address.event_id,
"m2mwitho2opks": [],
},
)

Expand Down Expand Up @@ -1040,6 +1147,7 @@ async def test_tournament(self):
"address": {
"event_id": self.address.pk,
"city": "Santa Monica",
"m2mwitho2opks": [],
"street": "Ocean",
},
},
Expand Down Expand Up @@ -1093,6 +1201,7 @@ async def test_team(self):
"address": {
"event_id": self.address.pk,
"city": "Santa Monica",
"m2mwitho2opks": [],
"street": "Ocean",
},
},
Expand Down
Loading

0 comments on commit 3d18d03

Please sign in to comment.