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

Insert entire list without duplication & UNIQUE constraint failed #716

Closed
RzTutul opened this issue Dec 13, 2022 · 5 comments
Closed

Insert entire list without duplication & UNIQUE constraint failed #716

RzTutul opened this issue Dec 13, 2022 · 5 comments
Assignees

Comments

@RzTutul
Copy link

RzTutul commented Dec 13, 2022

**Here I want to insert some static categories when the app will be lunch. the first time it was inserted successfully but the second when the app lunch it show "UNIQUE constraint failed". I want if categories name already exists then it should be ignored **
here is my code :

@database(version: 1, entities: [CategoriesEntity])
abstract class AppDatabase extends FloorDatabase {
HishabDao get hishabDao;
}

Entity
@entity(tableName : "cate_tbl",indices : [Index(value : ["cata_name"], unique : true)])
class CategoriesEntity {
@PrimaryKey(autoGenerate: true)
final int? id;
final String cata_name;
CategoriesEntity(this.id,this.cata_name);
}

DAO
@insert(onConflict : OnConflictStrategy.ignore)
Future insertCategories(List cate);

List
static final List listCate = [
CategoriesEntity(null,"Short"),
CategoriesEntity(null, "Medium"),
CategoriesEntity(null,"Large"),
];

void main() async{
WidgetsFlutterBinding.ensureInitialized();
final database = await $FloorAppDatabase.databaseBuilder('app_database.db').build();
final hishabDao = database.hishabDao;
await hishabDao.insertCategories(HelperUtils.listCate);
runApp(const MyApp());
}

ERROR

E/flutter (10713): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: DatabaseException(UNIQUE constraint failed: cate_tbl.cata_name (code 2067 SQLITE_CONSTRAINT_UNIQUE)) sql 'INSERT OR ABORT INTO cate_tbl (id, cata_name) VALUES (NULL, ?)' args [Small]

@RzTutul RzTutul changed the title Insert entire list without duplication Insert entire list without duplication & UNIQUE constraint failed Dec 13, 2022
@RzTutul
Copy link
Author

RzTutul commented Dec 14, 2022

@dkaera can you check this problem pls?

@dkaera
Copy link
Collaborator

dkaera commented Dec 14, 2022

@RzTutul
hmm, I think you need to set ids into your listCate

static final List listCate = [
CategoriesEntity(1,"Short"),
CategoriesEntity(2, "Medium"),
CategoriesEntity(3,"Large"),
];

I don't know why you are getting this error above, it needs to be investigated a bit.
but the option above should help solve the problem.

@RzTutul
Copy link
Author

RzTutul commented Dec 15, 2022

@dkaera But I set the id as the primary key with auto-increment
btw, if I provide id the occurred same error second time when the app lunch.

@dkaera
Copy link
Collaborator

dkaera commented Dec 15, 2022

@RzTutul just checked your integration
it works fine for me with null value for ID, please see stored DB records below:
image
stored data on init

static final List listCate = [
CategoriesEntity(null,"Short"),
CategoriesEntity(null, "Medium"),
CategoriesEntity(null,"Large"),
];

and then I'm adding random data by:

await catDao.insertCategories([CategoriesEntity(null, Random.secure().nextInt(100).hashCode.toString())]);

no errors in the logs, even after adding new values or restarting the application. Please check the generated code for creating the table structure, it should be like this:

        await database.execute(
            'CREATE TABLE IF NOT EXISTS `cate_tbl` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `cata_name` TEXT NOT NULL)');
        await database.execute(
            'CREATE UNIQUE INDEX `index_cate_tbl_cata_name` ON `cate_tbl` (`cata_name`)');

@dkaera dkaera self-assigned this Dec 16, 2022
@dkaera
Copy link
Collaborator

dkaera commented Jan 4, 2023

there are no updates here, so it looks like the problem is solved

@dkaera dkaera closed this as completed Jan 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants