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

mikro-orm migration:create command generates incorrect SQL with PostgreSQL #1033

Closed
pacome35220 opened this issue Nov 4, 2020 · 3 comments
Closed
Assignees

Comments

@pacome35220
Copy link
Contributor

Describe the bug

mikro-orm migration: create command generates incorrect SQL :
'create table "user" ("id" jsonb not null, "created_at" jsonb not null, "updated_at" jsonb not null);'

So when I want to insert a User, I get this stack trace because the js/ts object has a key 'created_at' which is a date, instead
a 'jsonb' as SQL expected (because 'jsonb' is the wrong SQL type generated).

[info] MikroORM successfully connected to database db on postgresql://db_user:*****@localhost:5432
[query] begin
[query] insert into "user" ("created_at", "id", "updated_at") values ('2020-11-04T15:38:12.147Z', '0f060e86-6447-4e76-ba36-fd1c3bf1d3ba', '2020-11-04T15:38:12.147Z') returning "id" [took 1 ms]
[query] rollback
DriverException: insert into "user" ("created_at", "id", "updated_at") values ('2020-11-04T15:38:12.147Z', '0f060e86-6447-4e76-ba36-fd1c3bf1d3ba', '2020-11-04T15:38:12.147Z') returning "id" - invalid input syntax for type json
    at PostgreSqlExceptionConverter.convertException (/home/foobar/mikro-orm-issue/node_modules/@mikro-orm/core/platforms/ExceptionConverter.js:8:16)
    at PostgreSqlExceptionConverter.convertException (/home/foobar/mikro-orm-issue/node_modules/@mikro-orm/postgresql/PostgreSqlExceptionConverter.js:40:22)
    at PostgreSqlDriver.convertException (/home/foobar/mikro-orm-issue/node_modules/@mikro-orm/core/drivers/DatabaseDriver.js:168:54)
    at /home/foobar/mikro-orm-issue/node_modules/@mikro-orm/core/drivers/DatabaseDriver.js:172:24
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async PostgreSqlDriver.nativeInsert (/home/foobar/mikro-orm-issue/node_modules/@mikro-orm/knex/AbstractSqlDriver.js:145:21)
    at async ChangeSetPersister.persistNewEntity (/home/foobar/mikro-orm-issue/node_modules/@mikro-orm/core/unit-of-work/ChangeSetPersister.js:55:21)
    at async ChangeSetPersister.executeInserts (/home/foobar/mikro-orm-issue/node_modules/@mikro-orm/core/unit-of-work/ChangeSetPersister.js:24:13)
    at async UnitOfWork.commitCreateChangeSets (/home/foobar/mikro-orm-issue/node_modules/@mikro-orm/core/unit-of-work/UnitOfWork.js:455:9)
    at async UnitOfWork.persistToDatabase (/home/foobar/mikro-orm-issue/node_modules/@mikro-orm/core/unit-of-work/UnitOfWork.js:417:13)

Here is the docker container postgres logs :

2020-11-04 15:33:51.645 UTC [72] ERROR:  invalid input syntax for type json at character 63
2020-11-04 15:33:51.645 UTC [72] DETAIL:  Expected end of input, but found "-11".
2020-11-04 15:33:51.645 UTC [72] CONTEXT:  JSON data, line 1: 2020-11...
2020-11-04 15:33:51.645 UTC [72] STATEMENT:  insert into "user" ("created_at", "id", "updated_at") values ('2020-11-04T15:33:51.514Z', '87c82e5f-b7f3-4fd5-ad4b-90adeec4e77e', '2020-11-04T15:33:51.514Z') returning "id"

To Reproduce
Steps to reproduce the behavior:

  1. git clone https://github.com/pacome35220/mikro-orm-issue

  2. cd mikro-orm-issue

  3. npm install

  4. tsc

  5. docker run --rm -e 'POSTGRES_USER=db_user' -e 'POSTGRES_PASSWORD=db_password' -e 'POSTGRES_DB=db' -p 5432:5432 postgres

  6. mikro-orm migration:create

  7. look the file in ./migrations/, the SQL is wrong

  8. Next you can run mikro-orm migration:up

  9. and finally the real bug is when node dist/main.js

Expected behavior
When mikro-orm was in version 3.6.15, the command generated:
'create table "user" ("id" varchar(255) not null, "created_at" timestamptz(0) not null, "updated_at" timestamptz(0) not null);'
It expected to generate same SQL in 4.2.3 i think.

Versions

Dependency Version
node 12.19.0
typescript 4.0.5
mikro-orm 4.2.4
your-driver postgresql
@B4nan
Copy link
Member

B4nan commented Nov 4, 2020

Duplicate of #990, you need to state the type explicitly when using property initializer (or use ts-morph).

@B4nan B4nan closed this as completed Nov 4, 2020
@pacome35220
Copy link
Contributor Author

pacome35220 commented Nov 4, 2020

@B4nan Thank you for your responsiveness, maybe this change should be explicitly mentioned in the evolution guide ?
https://mikro-orm.io/docs/upgrading-v3-to-v4/

@B4nan
Copy link
Member

B4nan commented Nov 4, 2020

Yeah looks like I forgot to mention the change of default metadata provider there, weird (it is only in the PR #475).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants