-
Notifications
You must be signed in to change notification settings - Fork 26
changes to support non-primary key in Piccolo Admin #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
6df9c07
d899cd1
1d9b3b2
b7e402d
346f3ee
a828396
a6f2cca
c94facb
ac16f2b
2c0a351
2cd11bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -423,14 +423,32 @@ async def get_ids(self, request: Request) -> Response: | |
|
|
||
| values = await query.run() | ||
|
|
||
| # if value_type not in or UUID | ||
| try: | ||
| target_pk_type = [ | ||
| i._meta.params["target_column"].value_type not in (int, uuid) | ||
| for i in self.table._meta._foreign_key_references | ||
| ][0] | ||
| except (AttributeError, IndexError): | ||
| target_pk_type = False | ||
|
|
||
| primary_key = self.table._meta.primary_key | ||
| if primary_key.value_type not in (int, str): | ||
| return JSONResponse( | ||
| {str(i[primary_key._meta.name]): i["readable"] for i in values} | ||
| { | ||
| str(i[primary_key._meta.name]): [ | ||
| i["readable"], | ||
| target_pk_type, | ||
| ] | ||
| for i in values | ||
| } | ||
| ) | ||
| else: | ||
| return JSONResponse( | ||
| {i[primary_key._meta.name]: i["readable"] for i in values} | ||
| { | ||
| i[primary_key._meta.name]: [i["readable"], target_pk_type] | ||
| for i in values | ||
| } | ||
| ) | ||
|
|
||
| ########################################################################### | ||
|
|
@@ -871,7 +889,16 @@ async def detail(self, request: Request) -> Response: | |
| try: | ||
| row_id = self.table._meta.primary_key.value_type(row_id) | ||
| except ValueError: | ||
| return Response("The ID is invalid", status_code=400) | ||
| for i in self.table._meta._foreign_key_references: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can move the logic around a bit here. target = i._meta.params.get("target_column")
if target is None:
try:
row_id = self.table._meta.primary_key.value_type(row_id)
except ValueError:
return Response("The ID is invalid", status_code=400)
else:
# insert your new logic here
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately this also does not work if we have multiple non-primary keys per single table. e.g if we add director name as non-primary key (or mix primary or non-primary keys) to review reviewer this does not work. we should to leave this as is. |
||
| target = i._meta.params["target_column"] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be |
||
| if target is not None: | ||
| reference_target_pk = ( | ||
| await self.table.select(self.table._meta.primary_key) | ||
| .where(target == row_id) | ||
| .first() | ||
| .run() | ||
| ) | ||
| row_id = reference_target_pk[self.table._meta.primary_key] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can do this slightly easier: reference_target_pk = (
await self.table.select(self.table._meta.primary_key)
.where(target == row_id)
.output(as_list=True)
.first()
.run()
)[0]
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately this does not work if we have multiple non-primary keys per single table. |
||
|
|
||
| if ( | ||
| not await self.table.exists() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| from unittest import TestCase | ||
|
|
||
| from piccolo.columns.column_types import ForeignKey, Varchar | ||
| from piccolo.table import Table | ||
| from starlette.testclient import TestClient | ||
|
|
||
| from piccolo_api.crud.endpoints import PiccoloCRUD | ||
|
|
||
|
|
||
| class Serie(Table): | ||
| name = Varchar(length=100, unique=True) | ||
|
|
||
|
|
||
| class Review(Table): | ||
| reviewer = Varchar() | ||
| serie = ForeignKey(Serie, target_column=Serie.name) | ||
|
|
||
|
|
||
| class TestTargetPK(TestCase): | ||
| """ | ||
| Make sure PiccoloCRUD works with Tables with a custom primary key column. | ||
| """ | ||
|
|
||
| def setUp(self): | ||
| Serie.create_table(if_not_exists=True).run_sync() | ||
| Review.create_table(if_not_exists=True).run_sync() | ||
|
|
||
| def tearDown(self): | ||
| Review.alter().drop_table().run_sync() | ||
| Serie.alter().drop_table().run_sync() | ||
|
|
||
| def test_target_column_pk(self): | ||
| serie = Serie(name="Devs") | ||
| serie.save().run_sync() | ||
| Review(reviewer="John Doe", serie=serie["name"]).save().run_sync() | ||
| review = Review.select(Review.serie.id).first().run_sync() | ||
|
|
||
| self.client = TestClient(PiccoloCRUD(table=Serie, read_only=False)) | ||
| response = self.client.get("/Devs/") | ||
| self.assertEqual(response.status_code, 200) | ||
| self.assertEqual(response.json()["id"], review["serie.id"]) |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see the idea. I think the problem though is it's having to do
[0]. If the_foreign_key_referencescontains more than 1 item, we're guessing that the first one is correct.I think we're going to have to add a GET parameter so the Piccolo Admin UI can explicitly tell us which column it's interested in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. That make sense. I will try to add a GET parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, thanks. I think another advantage of the GET param is if someone doesn't pass it we can return the same response as before, so there are no breaking API changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't get far, but we can pass
target_columnas a query paramand return the result like this
(we don't need to pass anything to query param and return all results because the
KeySearchModalalready needs all results)or without the
target_columnreturn standard responseI don't know yet how to trigger Vue frontend based on the existence of the
column_namequery parameter. I don't know is this ok, or I completely miss the point.