SeaQL / sea-orm

🐚 An async & dynamic ORM for Rust
https://www.sea-ql.org/SeaORM/
Apache License 2.0
6.98k stars 487 forks source link

Issue when using `on_conflict` with sqlite #2302

Open bWanShiTong opened 1 month ago

bWanShiTong commented 1 month ago

Description

Steps to Reproduce

  1. Create table with:

    CREATE TABLE IF NOT EXISTS heart_rate (
    unix BIGINT NOT NULL primary key,
    heart_rate smallint NOT NULL,
    rr TEXT NOT NULL
    );
    CREATE UNIQUE INDEX IF NOT EXISTS unix_index on heart_rate(unix);
  2. Run:

[tokio::main]

async fn main() -> Result<(), anyhow::Error> { let db_url = env::var("DATABASE_URL").expect("DATABASE_URL not set in env"); let pool = Database::connect(db_url).await?;

let model = heart_rate::ActiveModel {
    unix: Set(10000),
    heart_rate: Set(120),
    rr: Set(format!("{:?}", vec![100,312])),
};

heart_rate::Entity::insert(model)
    .on_conflict(OnConflict::column(heart_rate::Column::Unix))
    .do_nothing()
    .exec(&pool)
    .await?;
Ok(())

}

### Expected Behavior

To insert pass even if row with same unix exists

### Actual Behavior

Fail even if there are no items in table

```sh
[2024-07-27T17:11:24Z INFO  sqlx::query] summary="PRAGMA foreign_keys = ON; …" db.statement="\n\nPRAGMA foreign_keys = ON;\n" rows_affected=0 rows_returned=0 elapsed=93.502µs elapsed_secs=9.3502e-5
Error: Execution Error: error returned from database: (code: 1) incomplete input

Caused by:
    0: error returned from database: (code: 1) incomplete input
    1: error returned from database: (code: 1) incomplete input
    2: (code: 1) incomplete input

Reproduces How Often

Every time

Workarounds

No

Reproducible Example

See above

Versions

sea-orm = { version = "0.12.15", features = [
    "runtime-tokio-rustls",
    "sqlx-sqlite",
    "macros",
]}
bWanShiTong commented 1 month ago

Note:

Code bellow works

    heart_rate::Entity::insert(model)
        .on_conflict(
            OnConflict::column(heart_rate::Column::Unix)
                .do_nothing()
                .to_owned(),
        )
        .do_nothing()
        .exec(&pool)
        .await?;

But following doesn't:


    heart_rate::Entity::insert(model)
        .on_conflict(
            OnConflict::column(heart_rate::Column::Unix)
                .do_nothing()
                .to_owned(),
        )
        .exec(&pool)
        .await?;