Hello,
I really tried to figure this out but I'm completely stuck. I upgraded from moor 2.4 to 3.1 today and getting issues with one table which looks like this:
class Translations extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get langCode => text()();
TextColumn get key => text()();
TextColumn get value => text()();
@override
Set<Column> get primaryKey => {langCode, key};
}
I'm having an id which auto increments and a custom primaryKey set together by langCode and key.
If I'm now trying to insert or update my Translations I'm always getting:
"ERROR: InvalidDataException: Sorry, Instance of 'TranslationsCompanion' cannot be used for that because:
• id: This value was required, but isn't present"
Therefore I'm creating TranslationCompanions like this:
TranslationsCompanion toCompanion(TranslationEntry translationEntry) {
if (translationEntry == null) return null;
return TranslationsCompanion(
langCode: Value(translationEntry.langCode),
key: Value(translationEntry.key),
value: Value(translationEntry.value),
);
}
and adding them as batch
Future<void> addAllTranslations(
List<TranslationsCompanion> translationsList) async {
await clearDatabase();
return batch(
(batch) {
batch.insertAll(
translations,
translationsList,
mode: InsertMode.insertOrReplace,
);
},
);
}
Id should be auto incremented and therefore I dont need to pass it? It worked fine with moor 2.4 and I looked to the changelog and still don't understand what to fix here?
Best regards
Verena
Interesting, thanks for the report.
I'm not sure if that table is valid at all though. If I run the CREATE TABLE statement I'd expect moor to run here, I get an error from sqlite (it understandably complains about multiple primary keys).
CREATE TABLE translations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
lang_code TEXT,
"key" TEXT,
"value" TEXT,
PRIMARY KEY (lang_code, "key")
);
Obviously you were able to create the table with an older moor version, so I'll investigate what moor 2.4 did here.
Uh oh, the old moor version just ignored the primaryKey override in this case :(
The generator should emit a warning when using both autoIncrement() and primaryKey, I think that is impossible in sqlite.
The problem is that, in order to use AUTOINCREMENT, we have to make id an primary key. As a table can only have one primary key, {langCode, key} can't be another one. I'd recommend adding a unique constraint on these columns, which would have a similar effect.
Oh. I didn't know that, sorry! Then I'll use unique constraint as recommended.
Thanks very much for your fast answer!
Great!
I'll actually keep this open to track adding the error message.
Most helpful comment
Uh oh, the old moor version just ignored the
primaryKeyoverride in this case :(The generator should emit a warning when using both
autoIncrement()andprimaryKey, I think that is impossible in sqlite.The problem is that, in order to use
AUTOINCREMENT, we have to makeidan primary key. As a table can only have one primary key,{langCode, key}can't be another one. I'd recommend adding a unique constraint on these columns, which would have a similar effect.