Supporting foreign keys which reference non-primary keys#144
Supporting foreign keys which reference non-primary keys#144dantownsend wants to merge 3 commits intomasterfrom
Conversation
Codecov Report
@@ Coverage Diff @@
## master #144 +/- ##
=======================================
Coverage 98.41% 98.41%
=======================================
Files 3 3
Lines 189 189
=======================================
Hits 186 186
Misses 3 3 Continue to review full report at Codecov.
|
|
@dantownsend We need to change this line to this rowID: row[pkName]
and this line to this rowID: getKeySelectID(schema.primary_key_name)
With these two changes, everything is fine, and all that remains is to fix filter FK search. Of course, this needs to be double checked. |
|
@sinisaos Great, thanks. I'll try those changes. |
|
@dantownsend Forget about these changes because they are not good and they not work properly. Sorry. |
|
@dantownsend Sorry, but these changes don't work either. I can make the links work, but I need to change this https://github.com/piccolo-orm/piccolo_api/blob/f0db58c9961c8a182b35d71769d99aea5abbf190/piccolo_api/crud/endpoints.py#L872 try:
try:
row_id = self.table._meta.primary_key.value_type(row_id)
except ValueError:
for i in self.table._meta._foreign_key_references:
reference_target_pk = (
await self.table.select(self.table._meta.primary_key)
.where(i._meta.params["target_column"] == row_id)
.first()
.run()
)
row_id = reference_target_pk[self.table._meta.primary_key]
except ValueError:
return Response("The ID is invalid", status_code=400)in the Piccolo API. Changes in Piccolo Admin are not enough. |
| params: { | ||
| tableName: getTableName(name), | ||
| rowID: row[name] | ||
| rowID: row[pkName] |
There was a problem hiding this comment.
I also thought so before, but the rowID: row[pkName] is the primary key of the Review table, not Review.movie.id.
I wrote in the comment the changes I think we need to make in Piccolo API to get correct results.
There was a problem hiding this comment.
You're right - it took a while for me to realise this. My solution doesn't work.
|
@sinisaos Yeah, it's a tricky one. I had a look at it earlier, but still haven't fully wrapped my head around it yet. |
|
@dantownsend I agree that it is tricky and I hope there is a simpler way, but for now I do not see it. The changes in my first attempt and in your last commit are not good because they do not work both for primary key and non-primary foreign keys. Eventually I made the changes from the previous comment (with this change we get the primary key of table where the |
|
@sinisaos Damn - good catch. I didn't realise the BaseUser changes broke Piccolo Admin. Will look into fixing it ASAP. |
|
@dantownsend You need to change session auth Piccolo API tests. |
|
@sinisaos Yeah, you're right. Seems like there's an error when accessing |
|
@sinisaos I need to partially revert some of the changes to |
|
@sinisaos I've pushed a new release of Piccolo which solves the underlying problem. If you have time, are you able to do a fix for Piccolo API? Looks like the errors are because |
@sinisaos I'm thinking along the same lines. I was thinking we could pass a Would return something like: |
I can’t right now but I will do it as soon as I get home during the day. You are right, to fix test errors you just need to add target column to response schema. |
That's a good idea, I'll have to try that. My solution is that if the primary key is not and then in Piccolo Admin make for example |
|
@dantownsend I made PR in Piccolo API and with this changes you have to change 2 files in Piccolo Admin // in KeySearch.vue
methods: {
handleUpdate(event) {
this.selectedValue = event.readable[0]
this.hiddenSelectedValue =
event.readable[1] == false ? event.id : event.readable[0]
this.showModal = false
}
},and // KeySearchModal.vue
<li :key="id[0]" v-for="id in ids">
<a href="#" @click.prevent="selectResult(...id)">
{{ id[1][0] }}
</a>
</li>What do you think about all this? |
|
This PR has been marked as stale because it has been open for 30 days with no activity. Are there any blockers, or should this be closed? |
|
@dantownsend You should close this PR. Fix is here. |
|
This PR has been marked as stale because it has been open for 30 days with no activity. Are there any blockers, or should this be closed? |
|
@dantownsend You should close this PR. Fix is here. |
|
This PR has been marked as stale because it has been open for 30 days with no activity. Are there any blockers, or should this be closed? |
Related to piccolo-orm/piccolo#394