How to use customStatement with arguments?
I have a bunch of rows (~ 100k) to insert in various tables. For each table, I'm receiving the rows from an API as an array of values. So I'm thinking to do something like:
// channels is a List of channel
// a channel is an object with the following attributes:
// title: name of database table
// cols: list of columns
// values: list of rows
for (var channel in channels) {
final tableName = channel['title'];
final columns = List<String>.from(channel['cols']);
final query = 'INSERT INTO \'${tableName}\' (${columns.join(',')}) VALUES (${columns.map((_) => '?').join(',')})';
final valuesList = List<List<dynamic>>.from(channel['items']);
await transaction(() async {
for (var values in valuesList) {
// query is something like INSERT INTO 'author' (id,name) VALUES (?,?)
await customStatement(query, values);
}
});
}
However, when I run this, I get the error: SqfliteDatabaseException (DatabaseException(NOT NULL constraint failed: authors.id (Sqlite code 1299 SQLITE_CONSTRAINT_NOTNULL), (OS error - 2:No such file or directory)) sql 'INSERT INTO 'authors' (id,name) VALUES (?,?)' args []}).
I've double checked and I'm sure that values is never an empty array... How to fix this?
Huh, the code looks alright to me.
Can you try enabling logStatements: true on the VmDatabase or FlutterQueryExecutor and see if the expected query is executed with arguments? It would also be interesting to know if you're using moor_ffi or moor_flutter (and which version) so I can try to reproduce this, thanks!
Sorry for my incomplete report. I'm still using moor_flutter. Is it safe to migrate to ffi now? The migration page of the doc still says that if we don't have to, we should stay on moor_flutter as it is more stable.
In my pubspec.yaml I've got this under dependencies: moor: ^2.0.0
And in my pubspec.lock:
moor:
dependency: "direct main"
description:
name: moor
url: "https://pub.dartlang.org"
source: hosted
version: "2.3.0"
moor_flutter:
dependency: "direct main"
description:
name: moor_flutter
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
moor_generator:
dependency: "direct dev"
description:
name: moor_generator
url: "https://pub.dartlang.org"
source: hosted
version: "2.3.1"
I've set logStatements: true.
It's really weird because the log in VSCode shows:
I/flutter (21479): Moor: Sent INSERT OR REPLACE INTO 'peoplePeopleGroups' (peopleID,peopleGroupID) VALUES (?,?) with args [e16319e0-c03c-4e0b-, 1ddbe3e1-8cd4-47a6-]
But as I've ticked the checkbox to break on "Uncaught Exceptions" and I get the error: SqfliteDatabaseException (DatabaseException(NOT NULL constraint failed: peoplePeopleGroups.peopleID (Sqlite code 1299 SQLITE_CONSTRAINT_NOTNULL), (OS error - 2:No such file or directory)) sql 'INSERT OR REPLACE INTO 'peoplePeopleGroups' (peopleID,peopleGroupID) VALUES (?,?)' args []})
Yikes. https://github.com/simolus3/moor/blob/57fa1d50c66b6f8d6a68d10779e07a32a597b98a/moor_flutter/lib/moor_flutter.dart#L127-L130
I'll fix this now and add a test to make sure that doesn't happen again.
I'm still using moor_flutter. Is it safe to migrate to ffi now?
Thanks, I'll update the documentation. If you're on the latest stable Flutter version, moor_ffi should work fine, it's also much faster.
Fixed in moor_flutter 2.1.1