Skip to content

@Check constraint breaks schema generator #4505

@surfous

Description

@surfous

Describe the bug
SchemaGenerator (CLI or called programmatically) fails on subsequent runs when a check constraint is placed on a column.

Stack trace

TypeError: Cannot read properties of undefined (reading 'replace')
    at replaceKeyBindings (/Users/surf/Developer/git/comfortai-consumer/targets/server/node_modules/knex/lib/formatter/rawFormatter.js:46:23)
    at Raw.toSQL (/Users/surf/Developer/git/comfortai-consumer/targets/server/node_modules/knex/lib/raw.js:79:13)
    at Raw.Target.toQuery (/Users/surf/Developer/git/comfortai-consumer/targets/server/node_modules/knex/lib/builder-interface-augmenter.js:9:21)
    at Raw.toString (/Users/surf/Developer/git/comfortai-consumer/targets/server/node_modules/knex/lib/raw.js:70:17)
    at /Users/surf/Developer/git/comfortai-consumer/targets/server/node_modules/knex/lib/schema/tablecompiler.js:381:35
    at Array.map (<anonymous>)
    at TableCompiler_PG._addChecks (/Users/surf/Developer/git/comfortai-consumer/targets/server/node_modules/knex/lib/schema/tablecompiler.js:378:12)
    at TableCompiler_PG.createQuery (/Users/surf/Developer/git/comfortai-consumer/targets/server/node_modules/knex/lib/dialects/postgres/schema/pg-tablecompiler.js:49:13)
    at TableCompiler_PG.create (/Users/surf/Developer/git/comfortai-consumer/targets/server/node_modules/knex/lib/schema/tablecompiler.js:59:10)
    at TableCompiler_PG.toSQL (/Users/surf/Developer/git/comfortai-consumer/targets/server/node_modules/knex/lib/schema/tablecompiler.js:42:22)

To Reproduce
Steps to reproduce the behavior:

  1. Define property w/ check constraint:
@Property({ type: 'number', columnType: 'bigint', default: null, nullable: true })
@Check({ expression: columns => '"unsignedIntField" IS NULL OR ("unsignedIntField" >= 0 AND "unsignedIntField" <= 4294967295)' })
'unsignedIntField'?: number;
  1. Run schema:fresh - first run succeeds
npx mikro-orm schema:fresh --run
  1. Run schema:fresh again - this time it fails; repeated runs also fail
npx mikro-orm schema:fresh --run
  1. Modify the check expression
@Property({ type: 'number', columnType: 'bigint', default: null, nullable: true })
@Check({ expression: columns => '"unsignedIntField" IS NULL OR ("unsignedIntField" >= 0 AND "unsignedIntField" <= 4294967295)' })
'unsignedIntField'?: number;
  1. Run schema:fresh - succeeds again once
npx mikro-orm schema:fresh --run
  1. Run schema:fresh again - back to failure mode; repeated runs also fail
npx mikro-orm schema:fresh --run
  1. Comment/remove the check expression
@Property({ type: 'number', columnType: 'bigint', default: null, nullable: true })
// @Check({ expression: columns => '"unsignedIntField" IS NULL OR ("unsignedIntField" >= 0 AND "unsignedIntField" <= 4294967295)' })
'unsignedIntField'?: number;
  1. Run schema:fresh - back to success; repeated runs also succeed
npx mikro-orm schema:fresh --run

Expected behavior
Schema should drop/build successfully each time with the check constraints in place

Additional context
This feels similar to #4312 in behavior, but the stack trace is quite different.

Versions

Dependency Version
node 16.15.0
typescript 4.8.4 (also tried with 4.9.5)
mikro-orm 5.7.12
your-driver postgresql

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions