Skip to content
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

On conflict do nothing for MySQL #1790

Closed
tp971 opened this issue Jul 29, 2023 · 2 comments · Fixed by SeaQL/sea-query#765 or #2244
Closed

On conflict do nothing for MySQL #1790

tp971 opened this issue Jul 29, 2023 · 2 comments · Fixed by SeaQL/sea-query#765 or #2244
Assignees

Comments

@tp971
Copy link

tp971 commented Jul 29, 2023

Description

OnConflict::do_nothing() generates a syntactically incorrect query for MySQL/MariaDB:

something::Entity::insert(...)
    .on_conflict(OnConflict::new()
        .do_nothing()
        .to_owned()
    )
    .build(DbBackend::MySql)

returns something like:

INSERT INTO `something` (...) VALUES (...) ON DUPLICATE KEY DO NOTHING

which is not correct in MySQL/MariaDB. The correct syntax would be:

INSERT IGNORE INTO `something` (...) VALUES (...)

(cf. https://dev.mysql.com/doc/refman/8.1/en/insert.html and https://mariadb.com/kb/en/insert/)

Workarounds

Update the unique key on conflict:

something::Entity::insert(...)
    .on_conflict(OnConflict::new()
        .update_column(something::Column::Id)
        .to_owned()
    )
    .build(DbBackend::MySql)

Versions

0.12.1

darkmmon added a commit to darkmmon/sea-orm that referenced this issue Aug 18, 2023
tyt2y3 pushed a commit that referenced this issue Aug 18, 2023
* adds find_with_linked test

* WIP(related test)

* mock related test done

* complete relation test

* loader update

* find_with/also_related missing test case for empty from other side

* comments fixup

* revert loader test

* related select test done

* find with/also linked test cases

* removed due to it being functionally same as the new one

* fmt, remove excess import

* improved model generation

* issue related test case #1790

* added loader test cases and slight improvement to find_related/linked

* miscellaneous changes

* added empty insert, merge load_one test case

* completed loader many to many test case, fmt

* removed empty_insert test case for now

* commented insert_test

* added Cargo.toml for issue 1790's folder

* buffed salvo version for ci(0.49 yanked)

* revert version for salvo example
@darkmmon darkmmon mentioned this issue Aug 21, 2023
4 tasks
@tyt2y3
Copy link
Member

tyt2y3 commented Aug 22, 2023

I don't think INSERT IGNORE should be used, as semantically it is different from ON DUPLICATE KEY.
So ON DUPLICATE KEY UPDATE id=id is the way to go indeed.

Since SeaORM do know the PK of the entities, we are looking for ways to make it transparent.

@tyt2y3 tyt2y3 changed the title OnConflict::do_nothing() is broken under MySQL On conflict do nothing for MySQL Sep 8, 2023
@billy1624 billy1624 self-assigned this Apr 10, 2024
@tyt2y3 tyt2y3 reopened this Apr 14, 2024
@tyt2y3
Copy link
Member

tyt2y3 commented Apr 14, 2024

Now it's released in sea-query 0.31.0-rc.5, this can now be implemented in SeaORM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment