Skip to content

Commit c5a505a

Browse files
committed
fix(api): make database seeder resilient to missing optional fields
The database seeding process was crashing with a "Missing variable" error. This was caused by a conflict between the models' `toJson` method (which uses `includeIfNull: false` and omits keys for null values) and the `postgres` driver's requirement that all named SQL parameters exist in the provided parameters map. This change refactors the seeding logic to manually ensure that parameter maps for `categories` and `headlines` always contain keys for all optional/nullable columns (e.g., `description`, `icon_url`, `content`), setting their value to `null` if they are not already present. This makes the seeding process robust against incomplete fixture data and permanently resolves the startup error.
1 parent 39e4387 commit c5a505a

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

lib/src/services/database_seeding_service.dart

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,24 @@ class DatabaseSeedingService {
162162
_log.fine('Seeding categories...');
163163
for (final data in categoriesFixturesData) {
164164
final category = Category.fromJson(data);
165+
final params = category.toJson();
166+
167+
// Ensure optional fields exist for the postgres driver.
168+
// The driver requires all named parameters to be present in the map,
169+
// even if the value is null. The model's `toJson` with
170+
// `includeIfNull: false` will omit keys for null fields.
171+
params.putIfAbsent('description', () => null);
172+
params.putIfAbsent('icon_url', () => null);
173+
params.putIfAbsent('updated_at', () => null);
174+
165175
await _connection.execute(
166176
Sql.named(
167177
'INSERT INTO categories (id, name, description, icon_url, '
168178
'status, type, created_at, updated_at) VALUES (@id, @name, @description, '
169179
'@icon_url, @status, @type, @created_at, @updated_at) '
170180
'ON CONFLICT (id) DO NOTHING',
171181
),
172-
parameters: category.toJson(),
182+
parameters: params,
173183
);
174184
}
175185

@@ -203,6 +213,13 @@ class DatabaseSeedingService {
203213
_log.fine('Seeding headlines...');
204214
for (final data in headlinesFixturesData) {
205215
final headline = Headline.fromJson(data);
216+
final params = headline.toJson();
217+
218+
// Ensure optional fields exist for the postgres driver.
219+
params.putIfAbsent('description', () => null);
220+
params.putIfAbsent('content', () => null);
221+
params.putIfAbsent('updated_at', () => null);
222+
206223
await _connection.execute(
207224
Sql.named(
208225
'INSERT INTO headlines (id, title, source_id, category_id, '
@@ -211,7 +228,7 @@ class DatabaseSeedingService {
211228
'@published_at, @description, @content) '
212229
'ON CONFLICT (id) DO NOTHING',
213230
),
214-
parameters: headline.toJson(),
231+
parameters: params,
215232
);
216233
}
217234

0 commit comments

Comments
 (0)