-
Notifications
You must be signed in to change notification settings - Fork 192
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
Comments
@dkaera can you check this problem pls? |
@RzTutul 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. |
@dkaera But I set the id as the primary key with auto-increment |
@RzTutul just checked your integration 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`)'); |
there are no updates here, so it looks like the problem is solved |
**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]
The text was updated successfully, but these errors were encountered: