Jaguar-dart / jaguar_orm

Source-generated ORM with relations (one-to-one, one-to-many, many-to-many), preloading, cascading, polymorphic relations, etc
https://jaguar-dart.github.io
BSD 3-Clause "New" or "Revised" License
217 stars 52 forks source link

Update method association improvements #48

Closed jaumard closed 5 years ago

jaumard commented 6 years ago

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

tejainece commented 6 years ago

upsert you mean?

jaumard commented 6 years ago

Hum yes I guess (always forget this one and doing it into code lol)

tejainece commented 6 years ago

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
jaumard commented 6 years ago

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 :/

jaumard commented 6 years ago

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 :)

tejainece commented 6 years ago

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.

tejainece commented 6 years ago

Cascade also wont remove the items for you.

jaumard commented 6 years ago

that's true :) so do you have an idea on how fix this ?

jaumard commented 6 years ago

@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 :/