Closed jaumard closed 5 years ago
upsert
you mean?
Hum yes I guess (always forget this one and doing it into code lol)
How do you suggest that we implement upsert
? With a something like this?
IF NOT EXISTS (SELECT * FROM dbo.Employee WHERE ID = @SomeID)
INSERT INTO dbo.Employee(Col1, ..., ColN)
VALUES(Val1, .., ValN)
ELSE
UPDATE dbo.Employee
SET Col1 = Val1, Col2 = Val2, ...., ColN = ValN
WHERE ID = @SomeID
I guess this will work for postgres and mysql but I don't know for sqlite :/ but sqlite have a insert or replace that might do the job, but replace is different than update if you provide only few colums during the sql request :/
The easiest way would be to do it in the generated code ^^ if(id) bean.update else bean.insert
, I'm not good and familiar enough with SQL to help you with this part, we can try your solution and see if it run on sqflite after :)
We cannot use presence or absence of primary key to find if it is update or insert because sometimes people set their own primary keys and sometimes it is autogenerated.
Cascade also wont remove the items for you.
that's true :) so do you have an idea on how fix this ?
@tejainece bump here :) I'll soon need this feature as I'm implementing a data synchronization system, so if the data is already in database I want the ORM to replace the data for me, not just return an error that the data exist. I don't mind trying to implement this but for now I have no idea how :/
Currently the update method call update method of his children, the problem is when you update the parent you might have added new children, in that case it should call insert and not update. I know this one can be tricky as you don't know the schema of the children but it can be resolved by adding a insertOrUpdate method into bean, like this this one is aware of his own schema and can call insert or update accordingly