-
-
Notifications
You must be signed in to change notification settings - Fork 93
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
Insert command does not work when primary key is not AUTO_INCREMENT #109
Comments
This needs a custom command. The built in behavior depends on sequel datasets that are supposed to return PK values and it looks like it doesn't do that when the PK is not auto-generated. If you're blocked by this you can define your own command class and override class CreateWithCustomPk < ROM::SQL::Commands::Create
def insert(tuples)
tuples.each { |tuple| relation.insert(tuple) }
pks = tuples.map { |tuple| tuple.fetch(relation.primary_key) }
relation.where(relation.primary_key => pks).to_a
end
end Let me know if this works for you, we can try to figure out how to add that to rom-sql I think. |
The workaround you provided works as intended. |
@kwando thanks for testing it out @flash-gordon wdyt about adding a special command type for relations w/o an auto-incremental PK? it's not gonna be a common case but I'm sure it'll help people when they have to deal with legacy db schemas :) |
I think it's better to make it more straight-forward, that is if a PK value passed explicitly we'll use it for returning inserted records, how about that? :) |
@flash-gordon we can do that, although I'm not sure if I'm OK with extra code that is there just to handle something that is rarely the case :) What we could do is add ability to mark a PK as a non-auto increment and if that's the case, then we can extend commands with behavior that is needed to handle it properly. We already have a hook that extends command with additional behaviors, it receives command class and a dataset, so if we change it to receive a whole relation class then we can use its schema to see if we need to apply extensions. |
@solnic sounds like a plan, I like it |
Love to help with this one |
This is super low priority. I moved it to 2.1.0 milestone |
I have a table where the primary key is not generated by the database, I'm using MySQL.
When using a
Create
command to insert data it will return the wrong tuple.The text was updated successfully, but these errors were encountered: