Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@Check constraint breaks schema generator #4505

Closed
surfous opened this issue Jul 6, 2023 · 7 comments
Closed

@Check constraint breaks schema generator #4505

surfous opened this issue Jul 6, 2023 · 7 comments
Labels
bug Something isn't working

Comments

@surfous
Copy link

surfous commented Jul 6, 2023

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
@B4nan
Copy link
Member

B4nan commented Jul 15, 2023

I am not able to reproduce this, please provide a complete repro, ideally as a script (using schema generator programmatically). The stack trace is from creating the schema, so it needs to origin in the createSchema() call (as fresh command does dropSchema followed by createSchema).

@B4nan
Copy link
Member

B4nan commented Jul 21, 2023

Let's reopen with a failing repro, closing as not reproducible for now.

@B4nan B4nan closed this as not planned Won't fix, can't repro, duplicate, stale Jul 21, 2023
@kpervin
Copy link
Contributor

kpervin commented Aug 11, 2023

@B4nan I am also running into this issue with 5.7.12, but noticed that it only occurs when cache is present. Deleting the cache folder eliminates the issue. I will attempt to create a failing repo, but hopefully this one tidbit helps steer in the right direction in the meantime.

I also did some debugging and noticed that it seems metadata is generated properly, but somewhere down the line when TableCompiler_MySQL.toSQL (within the knex package) is called, the Check expression function provided is undefined.

@kpervin
Copy link
Contributor

kpervin commented Aug 14, 2023

@B4nan I have a failing representation of the issue here:

https://github.com/kpervin/MikroORM-Issue-Example-Repo/tree/4505-Check-constraint-breaks-schema-generator

Run test/schema-failing.spec.ts to see the issue.

@B4nan
Copy link
Member

B4nan commented Aug 14, 2023

Thanks, got it reproduced, its indeed about the metadata cache.

@B4nan B4nan reopened this Aug 14, 2023
@B4nan B4nan added bug Something isn't working and removed needs clarification labels Aug 14, 2023
@B4nan
Copy link
Member

B4nan commented Aug 14, 2023

The problem is actually the callback signature, as that is not properly serialized to the JSON.

@kpervin
Copy link
Contributor

kpervin commented Aug 24, 2023

@B4nan when you say the callback signature is not properly serialized, is that an issue with the serializer or with how one writes a Check expression?

@B4nan B4nan closed this as completed in 44d973e Aug 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants