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

Cannot execute increment on column defined by embedded entity #3182

Closed
smonv opened this issue Nov 30, 2018 · 4 comments
Closed

Cannot execute increment on column defined by embedded entity #3182

smonv opened this issue Nov 30, 2018 · 4 comments

Comments

@smonv
Copy link

smonv commented Nov 30, 2018

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
[x] 0.2.8 (or put your version here)

I have a embedded entity contain some friend statistics of a User. When user send a friend request to other user, I want increase friend.sent counter but the query always try to update null to frientSent.

My entity:

class FriendStats {
    @Column('bigint', { default: 0 })
    count: number;

    @Column('bigint', { default: 0 })
    sent: number;

    @Column('bigint', { default: 0 })
    received: number;
}

@Entity('users')
export class User {
    @Column(type => FriendStats)
    friend: FriendStats;
}

Query:

await this.manager.increment(User, { id: user.id }, 'friend.sent', 1);

TypeORM generated SQL:

UPDATE "users" SET "friendSent" = $2, "updatedAt" = CURRENT_TIMESTAMP WHERE "id" = $1 -- PARAMETERS: ["123456",null]

Error

query failed: UPDATE "users" SET "friendSent" = $2, "updatedAt" = CURRENT_TIMESTAMP WHERE "id" = $1 -- PARAMETERS: ["123456",null]

error: { error: null value in column "friendSent" violates not-null constraint ......
@apellizzn
Copy link
Contributor

apellizzn commented Nov 30, 2018

This seems a bit strange, considering that the following is the output of the increment function in the EntityManager.ts

await this.
createQueryBuilder(entityClass, "entity")
.update(entityClass)
.set({
  [propertyPath]: () => this.connection.driver.escape(column.databaseName) + " + " + Number(value)
})
.where(conditions)
.execute();

There should be at least a + in your query output!

@smonv
Copy link
Author

smonv commented Dec 1, 2018

I'm tried with createQueryBuilder and update but got same error and same generated query.

await manager.createQueryBuilder(User, 'u')
                .update()
                .set({
                    'friend.sent': () => `"friendSent" + ${Number(1)}`,
                })
                .where({ id: user.id }).execute();

Query:

UPDATE "users" SET "friendSent" = $2, "updatedAt" = CURRENT_TIMESTAMP WHERE "id" = $1 -- PARAMETERS: ["123456",null]

Error

error: { error: null value in column "friendSent" violates not-null constraint

@pleerock
Copy link
Member

We need a PR with a failing test for this issue.

@smonv
Copy link
Author

smonv commented Feb 9, 2019 via email

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

No branches or pull requests

5 participants