question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add onConflict() method to @PrimaryKey annotation

See original GitHub issue

DBFlow Version: 3.0.1 Issue Kind (Bug, Question, Feature): Feature

Description:

DBFlow does not support adding ON CONFLICT clause to @PrimaryKey annotation. Trying to add @Unique(onUniqueConflict = ConflictAction.REPLACE) to our primary key column does not seem to work. For example:

@Table(database = AppDatabase.class, name = "tbl_persona", allFields = true)
public class Persona extends BaseModel {

    @PrimaryKey
    @Unique(onUniqueConflict = ConflictAction.REPLACE)
    @Column(name = "id_persona")
    int idPersona;

    @NotNull
    String nombre;

    // ...
}

results in the following creation SQL query:

CREATE TABLE `tbl_persona`(`id_persona` INTEGER UNIQUE ON CONFLICT REPLACE,`nombre` TEXT NOT NULL, PRIMARY KEY(`id_persona`))

wich is not as expected.

According to SQLite documentation and own testing, the correct creation SQL query for the ON CONFLICT clause in primary key should be like this:

CREATE TABLE `tbl_persona`(`id_persona` INTEGER PRIMARY KEY ON CONFLICT REPLACE,`nombre` TEXT NOT NULL)

or like this:

CREATE TABLE `tbl_persona`(`id_persona` INTEGER,`nombre` TEXT NOT NULL, PRIMARY KEY(`id_persona`) ON CONFLICT REPLACE)

Any suggestion is welcome 😃

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
dleonettcommented, Jun 4, 2016

@agrosner I made it work for me by using insertConflict = ConflictAction.REPLACE in @Table annotation, and removing the @Unique annotation from my primary key column. It looks like this:

@Table(database = AppDatabase.class, name = "tbl_persona", allFields = true, insertConflict = ConflictAction.REPLACE)
public class Persona extends BaseModel {

    @PrimaryKey
    @Column(name = "id_persona")
    int idPersona;

    @NotNull
    String nombre;

    // ...
}
0reactions
luorenyucommented, Mar 4, 2017

@agrosner I encounter the same issue.but My DBFlow version is 4.0.0-beta5. and I try to add this annotation @Table(primaryKeyConflict = ConflictAction.REPLACE),but it doesn’t work .but @dleonett 's solution did work!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Conflict primary key - mysql - Stack Overflow
Conflict primary key ... AUTO means: choose the best strategy given the underlying database/table. Look at the generated SQL, and tell us which ......
Read more >
How to use `INSERT ON CONFLICT` to upsert data in ... - Prisma
PostgreSQL lets you either add or modify a record within a table depending on whether the record already exists. This is commonly known...
Read more >
The ON CONFLICT Clause - SQLite
The ON CONFLICT clause applies to UNIQUE, NOT NULL, CHECK, and PRIMARY KEY constraints. The ON CONFLICT algorithm does not apply to FOREIGN ......
Read more >
Use INSERT ON CONFLICT to overwrite data - AnalyticDB for ...
The INSERT ON CONFLICT statement allows you to update an existing row that contains a primary key when you execute the INSERT statement...
Read more >
OnConflictStrategy - Android Developers
OnConflict strategy constant to rollback the transaction. Inherited methods. From interface java.lang.annotation.Annotation ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found