Skip to content

Select fields with defaultValue generate as NOT NULL - breaks migration with NULL data #304

@borisno2

Description

@borisno2

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 values

Affected Fields (in our migration):

  • Instrument.status
  • Lesson.status
  • LessonLength.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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions