Skip to content

Commit 3dc1109

Browse files
committed
fix(api): correct seeding order to respect foreign key constraints
The database seeding was failing with a foreign key constraint violation because it attempted to seed `sources` before `countries`. The `sources` table has a foreign key dependency on the `countries` table. This change reorders the operations in `seedGlobalFixtureData`, ensuring that `countries` are seeded before `sources`, which resolves the error.
1 parent 246f8b0 commit 3dc1109

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

lib/src/services/database_seeding_service.dart

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,19 @@ class DatabaseSeedingService {
190190
);
191191
}
192192

193+
// Seed Countries (must be done before sources and headlines)
194+
_log.fine('Seeding countries...');
195+
for (final data in countriesFixturesData) {
196+
final country = Country.fromJson(data);
197+
await _connection.execute(
198+
Sql.named(
199+
'INSERT INTO countries (id, name, code) '
200+
'VALUES (@id, @name, @code) ON CONFLICT (id) DO NOTHING',
201+
),
202+
parameters: country.toJson(),
203+
);
204+
}
205+
193206
// Seed Sources
194207
_log.fine('Seeding sources...');
195208
for (final data in sourcesFixturesData) {
@@ -226,19 +239,6 @@ class DatabaseSeedingService {
226239
);
227240
}
228241

229-
// Seed Countries
230-
_log.fine('Seeding countries...');
231-
for (final data in countriesFixturesData) {
232-
final country = Country.fromJson(data);
233-
await _connection.execute(
234-
Sql.named(
235-
'INSERT INTO countries (id, name, code) '
236-
'VALUES (@id, @name, @code) ON CONFLICT (id) DO NOTHING',
237-
),
238-
parameters: country.toJson(),
239-
);
240-
}
241-
242242
// Seed Headlines
243243
_log.fine('Seeding headlines...');
244244
for (final data in headlinesFixturesData) {

0 commit comments

Comments
 (0)