-
-
Notifications
You must be signed in to change notification settings - Fork 611
Description
Describe the bug
When using a raw query fragment in a check constraint, such as with sql or the new quote, the generated constraint in migrations are unexpected.
Given example check expression name_max_length:
e => sql`char_length(${e.name}) <= ${NAME_MAX_LENGTH}`In the case of @Check, the migration creates successfully, but creates invalid SQL:
... add constraint name_max_length check([raw]: char_length(??) <= ?? (#0));
This looks like the result of calling .toString() on RawQueryFragment.
In the case of using the check field of @Property, the migration simply fails to create with:
TypeError: str?.replace is not a function
at simplify (/workspaces/app/node_modules/@mikro-orm/knex/schema/SchemaComparator.js:510:19)
at SchemaComparator.diffExpression (/workspaces/app/node_modules/@mikro-orm/knex/schema/SchemaComparator.js:514:36)
at SchemaComparator.diffTable (/workspaces/app/node_modules/@mikro-orm/knex/schema/SchemaComparator.js:240:23)
at SchemaComparator.compare (/workspaces/app/node_modules/@mikro-orm/knex/schema/SchemaComparator.js:74:47)
at SqlSchemaGenerator.getUpdateSchemaMigrationSQL (/workspaces/app/node_modules/@mikro-orm/knex/schema/SqlSchemaGenerator.js:178:35)
at async Migrator.getSchemaDiff (/workspaces/app/node_modules/@mikro-orm/migrations/Migrator.js:278:26)
at async Migrator.createMigration (/workspaces/app/node_modules/@mikro-orm/migrations/Migrator.js:54:22)
at async Function.handleCreateCommand (/workspaces/app/node_modules/@mikro-orm/cli/commands/MigrationCommandFactory.js:147:21)
at async Function.handleMigrationCommand (/workspaces/app/node_modules/@mikro-orm/cli/commands/MigrationCommandFactory.js:89:17)
The documentation gives an example of using quote with @Index, and implies it is usable in other places, but apparently not with check constraints.
Reproduction
const NAME_MAX_LENGTH = 64;
@Entity()
class ExampleDecoratorEntity {
@Property()
@Check({
name: "name_max_length",
expression: e => sql`char_length(${e.name}) <= ${NAME_MAX_LENGTH}`
})
name!: string;
}
@Entity()
class ExampleFieldEntity {
@Property({
check: e => sql`char_length(${e.name}) <= ${NAME_MAX_LENGTH}`
})
name!: string;
}What driver are you using?
PostgreSQL
MikroORM version
"@mikro-orm/core": "6.5.7", "@mikro-orm/migrations": "6.5.7", "@mikro-orm/nestjs": "6.1.1", "@mikro-orm/postgresql": "6.5.7", "@mikro-orm/sql-highlighter": "1.0.1"
Node.js version
v22.20.0
Operating system
Debian GNU/Linux 12 (bookworm) - VSCode DevContainer
Validations
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord.
- The provided reproduction is a minimal reproducible example of the bug.