Add onConflict() method to @PrimaryKey annotation
See original GitHub issueDBFlow 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:
- Created 7 years ago
- Comments:7 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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:@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!