Search code examples
fluttersqflite

sqflite insert return zero


In other places, database operations work perfectly but in one specific table insert operation returns 0 and no row is being inserted and no error produces.

I'm using version sqflite: ^2.0.2+1. sale.serial = await db.insert(tableSale, sale.toMap());

Table creation code is:

db.execute('''
          create table $tableSale ( 
            $columnSerial integer primary key autoincrement, 
            $columnFbId integer not null,
            $columnBillID integer not null,
            $columnAmount real not null,
            $columnBalance real DEFAULT 0,
            $columnDiscount real DEFAULT 0,
            $columnFreight real DEFAULT 0,
            $columnAddedBy text not null,
            $columnDate text not null,
            $columnDateApproved integer,
            $columnFromPhone text not null,
            $columnToPhone text not null,
            $columnNote text,
            $columnDetail text,
            $columnItems text,
            UNIQUE($columnBillID) ON CONFLICT IGNORE)
          ''');

Solution

  • I found the problem. There is UNIQUE($columnFbId) ON CONFLICT IGNORE) in the table creation code and on conflict, the insert operation is being simply ignored with no Flutter or database error.

    I found this by adding ConflictAlgorithm. The code is: sale.serial = await db.insert(tableSale, sale.toMap(), conflictAlgorithm: ConflictAlgorithm.fail)

    Thanks @nitishk72 for the hint.