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

invalid serialization of json during upsert #3787

Closed
boredland opened this issue Nov 23, 2022 · 5 comments
Closed

invalid serialization of json during upsert #3787

boredland opened this issue Nov 23, 2022 · 5 comments
Labels
bug Something isn't working

Comments

@boredland
Copy link
Contributor

boredland commented Nov 23, 2022

Describe the bug

we discovered that during upsert a json property is being serialized into a string. this test demonstrates that behaviour.

To Reproduce
Steps to reproduce the behavior:

#3786

Versions

Dependency Version
node 16.18.0
typescript 4.9.3
mikro-orm 5.5.3

we were able to reproduce this with sqlite, better-sqlite and postgres drivers (didn't test others).

@B4nan B4nan added the bug Something isn't working label Nov 28, 2022
@B4nan B4nan closed this as completed in 434d417 Dec 3, 2022
@darioielardi
Copy link

Unfortunately, we are still seeing this with v5.6.0.

@B4nan
Copy link
Member

B4nan commented Dec 14, 2022

Then please provide a complete repro, the fix included a passing test case 🤷

@Zmaon
Copy link

Zmaon commented Feb 15, 2023

I just reproduced the same issue with upsertMany using Mikro-orm 5.6.9.
For me it happens on any JSON column when the type is set like this in the entity:

@Property({ type: 'json' })

If settings:

@Property({ columnType: 'json' })

then the problem is gone.

@B4nan
Copy link
Member

B4nan commented Feb 15, 2023

Then provide the repro please, both should behave exactly the same since 5.6.9, before the type: 'json' is what you actually need, as that is what automatically uses the JsonType - and that is what you need to have correct JSON property handling.

@Zmaon
Copy link

Zmaon commented Feb 16, 2023

Ok I wrote a simple test that does nothing but create two tables and insert data into them. For some reason I wasn't able to just set columnType='json' in MyEntity2, I had to also add type='json' which is not the case in my other projects. The behaviour is the same anyway. I hope this helps.

package.json:

{
  "devDependencies": {
    "@babel/plugin-proposal-decorators": "^7.20.13",
    "@types/jest": "^29.4.0",
    "jest": "^29.4.3",
    "ts-jest": "^29.0.5",
    "typescript": "^4.9.5"
  },
  "dependencies": {
    "@mikro-orm/core": "^5.6.9",
    "@mikro-orm/mysql": "^5.6.9"
  }
}

mo.test.ts

import { Entity, PrimaryKey, Property } from "@mikro-orm/core";
import { MikroORM } from "@mikro-orm/mysql";

@Entity()
class MyEntity1 {
  @PrimaryKey({ type: 'number' })
  id!: number;

  @Property({ type: 'json' })
  field!: object;
}

@Entity()
class MyEntity2 {
  @PrimaryKey({ type: 'number' })
  id!: number;

  @Property({ type: 'json', columnType: 'json' })
  field!: object;
}

test('mikro-orm JSON serialization with upsertMany', async() => {
  const orm = await MikroORM.init({
    type: 'mysql',
    dbName: 'motest',
    host: '127.0.0.1',
    user: 'root',
    password: 'root',
    port: 3378,
    allowGlobalContext: true,
    entities: [MyEntity1, MyEntity2]
  });
  await orm.schema.refreshDatabase();
  const em = orm.em;

  const entity1 = em.create(MyEntity1, {
    id: 1,
    field: {
      firstName: 'John',
      lastName: 'Doe'
    }
  });
  em.upsertMany([entity1]);
  const entity2 = em.create(MyEntity2, {
    id: 1,
    field: {
      firstName: 'Albert',
      lastName: 'Doe'
    }
  });
  em.upsertMany([entity2]);
});

Looking into the database I see that for my_entity1:

image

and for my_entity2:

image

@B4nan B4nan reopened this Feb 16, 2023
@B4nan B4nan closed this as completed in 3928e78 Feb 17, 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

4 participants