diff --git a/floor_generator/lib/processor/entity_processor.dart b/floor_generator/lib/processor/entity_processor.dart index d4bc73c2..a4f59dd8 100644 --- a/floor_generator/lib/processor/entity_processor.dart +++ b/floor_generator/lib/processor/entity_processor.dart @@ -163,23 +163,20 @@ class EntityProcessor extends QueryableProcessor { // can't happen as Index.unique is non-nullable if (unique == null) throw ArgumentError.notNull(); - final values = indexObject + final indexColumnNames = indexObject .getField(IndexField.value) ?.toListValue() ?.mapNotNull((valueObject) => valueObject.toStringValue()) .toList(); - if (values == null || values.isEmpty) { + if (indexColumnNames == null || indexColumnNames.isEmpty) { throw _processorError.missingIndexColumnName; } - final indexColumnNames = fields - .map((field) => field.columnName) - .where((columnName) => values.any((value) => value == columnName)) - .toList(); - - if (indexColumnNames.isEmpty) { - throw _processorError.noMatchingColumn(values); + for (final indexColumnName in indexColumnNames) { + if (!fields.any((field) => field.columnName == indexColumnName)) { + throw _processorError.noMatchingColumn(indexColumnName); + } } final name = indexObject.getField(IndexField.name)?.toStringValue() ?? diff --git a/floor_generator/lib/processor/error/entity_processor_error.dart b/floor_generator/lib/processor/error/entity_processor_error.dart index 85c4b605..63f9ebee 100644 --- a/floor_generator/lib/processor/error/entity_processor_error.dart +++ b/floor_generator/lib/processor/error/entity_processor_error.dart @@ -60,10 +60,10 @@ class EntityProcessorError { } InvalidGenerationSourceError noMatchingColumn( - final List columnNames, + final String columnName, ) { return InvalidGenerationSourceError( - 'No matching columns found for the given index. (${columnNames.join(', ')})', + 'No matching column found for the given index. (`$columnName`)', todo: "Make sure to add a correct index column name like: Index(values: ['foo'])').", element: _classElement, diff --git a/floor_generator/test/processor/entity_processor_test.dart b/floor_generator/test/processor/entity_processor_test.dart index 476de063..873b6761 100644 --- a/floor_generator/test/processor/entity_processor_test.dart +++ b/floor_generator/test/processor/entity_processor_test.dart @@ -144,7 +144,7 @@ void main() { test('Process entity with index', () async { final classElement = await createClassElement(''' - @Entity(indices: [Index(name:'i1', unique: true, value:['id']),Index(unique: false, value:['id','name'])]) + @Entity(indices: [Index(name:'i1', unique: true, value:['id']),Index(unique: false, value:['name', 'id'])]) class Person { @primaryKey final int id; @@ -165,7 +165,7 @@ void main() { const foreignKeys = []; final indices = [ Index('i1', 'Person', true, ['id']), - Index('index_Person_id_name', 'Person', false, ['id', 'name']) + Index('index_Person_name_id', 'Person', false, ['name', 'id']) ]; const constructor = "Person(row['id'] as int, row['name'] as String)"; const valueMapping = "{'id': item.id, 'name': item.name}"; @@ -630,7 +630,7 @@ void main() { test('no matching index column', () async { final classElement = await createClassElement(''' @Entity( - indices:[Index(value:['notAColumn'])] + indices:[Index(value:['id', 'notAColumn'])] ) class Dog { @primaryKey @@ -649,7 +649,7 @@ void main() { expect( processor.process, throwsInvalidGenerationSourceError(EntityProcessorError(classElement) - .noMatchingColumn(['notAColumn']))); + .noMatchingColumn('notAColumn'))); }); test('auto-increment not usable with `WITHOUT ROWID`', () async { final classElement = await createClassElement('''