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

json and jsonb comparison does not work #3496

Closed
grin0c opened this issue Jan 23, 2019 · 7 comments
Closed

json and jsonb comparison does not work #3496

grin0c opened this issue Jan 23, 2019 · 7 comments

Comments

@grin0c
Copy link
Contributor

grin0c commented Jan 23, 2019

Issue type:

[ ] question
[x] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[x] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[ ] latest
[ ] @next
[ 0.2.8] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:
If we save such an object in the database:

[{"message": "", "attributeKey": "files", "level": "wrong"}]

then postgresql saves this object as:

[{"level": "wrong", "message": "", "attributeKey": "files"}]

The problem is here: https://github.com/typeorm/typeorm/blob/master/src/persistence/SubjectChangedColumnsComputer.ts#L80-L83

@pleerock
Copy link
Member

please provide more details including minimal entity code for reproduction.

@grin0c
Copy link
Contributor Author

grin0c commented Jan 23, 2019

@Entity()
export class Post {
  @PrimaryGeneratedColumn()
  id: number

  @UpdateDateColumn({ type: 'timestamptz' })
  updatedAt: Date

  @CreateDateColumn({ type: 'timestamptz' })
  createdAt: Date

  @Column({ type: 'jsonb', nullable: true })
  problems: object
}

const connection = await createConnection(connectionOptions)
const repository = connection.getRepository(Post)

const p = await repository.save(repository.create({ problems: [{ attributeKey: 'files', level: 'wrong', message: '' }] }))

await repository.save(p)

The second save method is to run update.

@pleerock
Copy link
Member

The problem is here:

can you please explain why problem is in there? Json.stringify doesn't change properties order, right?

@grin0c
Copy link
Contributor Author

grin0c commented Jan 24, 2019

The order of keys changes postgres when saves json.
2019-01-24 10 48 06

When save requests a model from the database, the keys in json come in a different order and are obtained:

JSON.stringify([{"message": "", "attributeKey": "files", "level": "wrong"}]) !== JSON.stringify([{"level": "wrong", "message": "", "attributeKey": "files"}])

@pleerock
Copy link
Member

ah okay, problem is that comparison doesn't work properly. If you could describe it in details I could understand it more easily from the beginning. How do you suggest to make this comparison then?

@grin0c
Copy link
Contributor Author

grin0c commented Jan 25, 2019

} else if (column.type === "json" || column.type === "jsonb") {
    if (OrmUtils.deepCompare(entityValue, databaseValue)) return;
}

@pleerock
Copy link
Member

pleerock commented Jan 25, 2019

okay, PR it. Make sure to make it against all drivers. Also add a comment why json.stringify doesn't work

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