Skip to content

when inserting I want a way to set the "conflict" behavior. #174

@lex148

Description

@lex148

Discussed in #173

Originally posted by bbenne10 February 26, 2026
Currently, I don't see a great way to ignore conflicts on insert.

I have a File table that looks like this:

#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
pub struct Sha256Sum([u8; SHA256_BYTE_LEN]);

// And some traits to make sqlx happy...
impl sqlx::Type<sqlx::Postgres> for Sha256Sum {...}
impl<'r> sqlx::Decode<'r, sqlx::Postgres> for Sha256Sum {...}
impl<'q> sqlx::Encode<'q, sqlx::Postgres> for Sha256Sum {...}


#[derive(WeldsModel)]
struct File {
  sha256: Sha256Sum,
  bytes: Vec<u8>,
}

Asking the DB if this File exists has a race condition (as we're not using linearizable transactions), so we currently write some manual SQL to ignore the failure to insert if the sha256 collides:

INSERT INTO file(sha256, bytes) VALUES (?, ?) ON CONFLICT (sha256) DO NOTHING

But raw SQL is a code smell (as it means you're not able to use the type system to tell immediately if the currently represented schema is upheld in all consuming code), so I would like to back this code out if possible. I see no way to currently be able to do this.

Does such a thing exist?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions