Skip to content

Commit 3ff1b60

Browse files
committed
fix(api): align headlines table schema with model
Updated the `headlines` table schema to allow `source_id` and `category_id` to be nullable, which correctly reflects the optional `source` and `category` fields in the shared `Headline` model. Foreign key constraints have been added to these columns to maintain referential integrity when a value is present. The database seeding logic has also been updated to handle the potential for null values in these fields during fixture insertion.
1 parent e9ffedf commit 3ff1b60

File tree

1 file changed

+5
-14
lines changed

1 file changed

+5
-14
lines changed

lib/src/services/database_seeding_service.dart

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ class DatabaseSeedingService {
106106
CREATE TABLE IF NOT EXISTS headlines (
107107
id TEXT PRIMARY KEY,
108108
title TEXT NOT NULL,
109-
source_id TEXT NOT NULL,
110-
category_id TEXT NOT NULL,
109+
source_id TEXT REFERENCES sources(id),
110+
category_id TEXT REFERENCES categories(id),
111111
image_url TEXT,
112112
url TEXT,
113113
published_at TIMESTAMPTZ,
@@ -260,19 +260,10 @@ class DatabaseSeedingService {
260260
final headline = Headline.fromJson(data);
261261
final params = headline.toJson();
262262

263-
// The `source_id` and `category_id` columns are NOT NULL. If a fixture
264-
// is missing the nested source or category object, we cannot proceed.
265-
if (headline.source == null || headline.category == null) {
266-
_log.warning(
267-
'Skipping headline fixture with missing source or category ID: '
268-
'${headline.title}',
269-
);
270-
continue;
271-
}
272-
273263
// Extract IDs from nested objects and remove the objects to match schema.
274-
params['source_id'] = headline.source!.id;
275-
params['category_id'] = headline.category!.id;
264+
// These are now nullable to match the schema.
265+
params['source_id'] = headline.source?.id;
266+
params['category_id'] = headline.category?.id;
276267
params.remove('source');
277268
params.remove('category');
278269

0 commit comments

Comments
 (0)