-
Notifications
You must be signed in to change notification settings - Fork 48
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
optionally return PKs? #168
Comments
Here's a bit of code that will do that. I had considered adding this into my PR #156 but that PR seems to have stalled. If there's an interest in adding this functionality, I don't mind submitting a separate PR, @palewire First, need to subclass CopyMapping:
Unfortunately, I don't believe it's possible to implement this "ResultsCopyMapping" the "normal way" (e.g., by putting
Then, finally, on your model you do
This code is slightly different than the implementation I have that's working for me, so let me know if you have any issues with it! |
thanks! That's really helpful :) If an API is added for this, my vote would be for that to be able to return either i) raw IDs (i.e. avoid the SELECT in your code above) or ii) full ORM instances (as in your example) Also I think option ii) could be made more efficient by selecting the last N rows in the table, where N is the number of rows inserted, rather than doing the selection by |
Definitely! In my case, just grabbing the last If you just want to get the ids, just change
|
aha thanks I did not consider how the "last N" would interact with "upsert" :) hope some progress on that PR can made too |
What would the PR be? Adding a post_insert hook? |
I'm not 100% on the best implementation API-wise. The necessary hooks are all already there ( |
Gotcha. If this is simply a common extra bit to slap into the hook, I don't think it needs to be integrated into the repo. Sharing the snippet seems like enough. I'd be happy to include the example in the docs as a cookbook style snippet. |
Yeah, I agree that's probably the best approach. I don't mind starting a PR on it. What do you think about appending it to the "Extending with hooks" section? |
I understand if you would prefer not to maintain this in the repo (e.g. due to time constraints, not being core functionality etc). my opinion (which you are welcome to ignore 😉 ) is that as this functionality is provided by django out of the box it would make this repo stronger by doing the same instead of providing a harder-to-find code snippet containing SQL manipulation... as I said just my opinion - obviously just trying to change your mind - and understand if you decide against. Thanks! |
Can you point me to where Django implements it? If we can include something that 100% matches a public API in Django I would reconsider. |
django's Actually the django docs are not very clear that the PK is set on the returned objects, but it is :) Relevant django ticket for this is here: https://code.djangoproject.com/ticket/19527 Thanks! |
Would it be possible to provide an option to return PKs of inserted objects?
Given that INSERT is used to put data into the destination table after COPY to the temporary table, it looks like it should be possible via a "RETURNING id" (or whatever the PK field name is)?
Thanks! :)
The text was updated successfully, but these errors were encountered: