-
Notifications
You must be signed in to change notification settings - Fork 890
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
Possible to clear a field in a bulk Upsert? #2846
Comments
Just noticed a similar question was asked here before: #1979 I see a solution for always setting a field to |
Hey @rafeyfanwar 👋
Can you elaborate, please? What do you expect the Ent API and the underlying SQL statement to look like? |
Hey @a8m! Sure thing. Here's an example of what i'm trying to achieve. Let's say i start out with the following table:
I want to bulk upsert such that I can set the email field for row with
I want the result of this operation to be for the table to be updated to this:
I'm admittedly not familiar with SQL in this much detail, but i believe the intended underlying SQL statement would be something like this:
This example is simplification btw -- the real logic loops through many lines and requires multiple columns to be updated in this way (potentially to a new value, potentially to NULL). Let me know if i can elaborate/clarify further, thanks! |
Hey @rafeyfanwar, client.T.CreateBulk(
client.T.Create().SetID(10), // Will set "email" to "<nil>" (NULL).
client.T.Create().SetID(20).SetEmail("X"), // Will set "email" to "X".
).
OnConflict().
UpdateNewValues().
ExecX(ctx)
// query: INSERT INTO `t` (`id`, `key`) VALUES (?, ?), (?, ?) ON DUPLICATE KEY UPDATE `id` = `t`.`id`, `key` = VALUES(`key`)
// args: [10, <nil>, 20, "X"] One thing though, is that current statements pass |
@a8m thanks for the additional context!
Is there a way to modify your code snippet above such that it will set an existing |
Are you able to reproduce this? Because I'm not. Please, see #2849. |
I decided to inline Closing, but please feel free to reopen it if you still have issues with it. Also, you're more than welcome to join our Discord community 😎 |
I've generated the go files for my project using the
--feature sql/upsert
flag and it works for most of what i'm trying to do, but i haven't been able to figure out if/how i can set a field toNULL
in an upsert.I initially tried using the
SetNillable
setters, which i realized ignorenil
values rather than clearing that column in that row. I see that there areClearX()
functions that can be set onUpdate
s, but since upserts useCreate
s i don't have access to those functions.Is there any way i can clear
nil
fields on an upsert?The text was updated successfully, but these errors were encountered: