Open kwando opened 7 years ago
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 insert
ie:
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? :) P.S. You can't rely on the presence of auto-incremental flag or something because there can be triggers which can do literally anything, including using custom sequences or generating PKs from other column values.
@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.