-
-
Notifications
You must be signed in to change notification settings - Fork 0
Description
Summary
When migrating from Keystone to OpenSaaS Stack, select fields that have defaultValue but aren't explicitly marked as required generate Prisma schema fields as NOT NULL. This causes migration failures when existing database data contains NULL values.
Current Behavior
Select fields with defaultValue generate as required (NOT NULL) in Prisma schema:
// OpenSaaS Stack field definition
status: select({
options: [
{ label: 'Draft', value: 'DRAFT' },
{ label: 'Active', value: 'ACTIVE' },
{ label: 'Archived', value: 'ARCHIVED' },
],
defaultValue: 'ACTIVE',
// No validation: { isRequired: true }
})Generates:
status String @default("ACTIVE") // NOT NULL - no ?Migration Impact
If legacy data contains NULL values, the migration fails:
ALTER COLUMN "status" SET NOT NULL;
-- Error: column "status" contains null valuesAffected Fields (in our migration):
Instrument.statusLesson.statusLessonLength.status
Root Cause
The schema generation logic treats defaultValue presence as implying required/NOT NULL, which differs from Keystone's behavior where:
- Keystone:
defaultValue= provide default on create, but allow NULL in DB - OpenSaaS:
defaultValue= NOT NULL constraint + default value
Workaround
Update NULL values to defaults before running migration:
UPDATE "Instrument" SET status = 'ACTIVE' WHERE status IS NULL;
UPDATE "Lesson" SET status = 'ACTIVE' WHERE status IS NULL;
UPDATE "LessonLength" SET status = 'ACTIVE' WHERE status IS NULL;Proposed Solutions
Option 1: Add Explicit Nullable Option (Recommended)
Add a nullable option to select fields:
status: select({
options: [...],
defaultValue: 'ACTIVE',
db: { isNullable: true } // Allow NULL despite default
})Option 2: Change Default Behavior (Breaking)
Make defaultValue alone generate nullable fields, require explicit validation: { isRequired: true } for NOT NULL.
Option 3: Documentation
Document that defaultValue implies NOT NULL and provide migration guide.
Priority
Medium - Has clear workarounds but affects Keystone-to-OpenSaaS migrations.