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

Query builder update generates broken SQL #2849

Closed
marano opened this issue Sep 25, 2018 · 8 comments · Fixed by #3324
Closed

Query builder update generates broken SQL #2849

marano opened this issue Sep 25, 2018 · 8 comments · Fixed by #3324

Comments

@marano
Copy link

marano commented Sep 25, 2018

Issue type:

[x] bug report

Database system/driver:

[x] postgres

TypeORM version:

[x] latest

const ids = [1]
const query = manager
  .createQueryBuilder()
  .update(Entity)
  .set({ brandId: 2 })
  .where(`"brandId" IN (:...ids)`, { ids })
console.log(query.getSql())

This logs:

query: UPDATE "image_classification" SET WHERE "brandId" IN ($1) -- PARAMETERS: [1]

Expected result:
Generated SQL to include object passed to set method.

@marano
Copy link
Author

marano commented Sep 26, 2018

I've figured this works:

const brand = await Brand.findOne(2)
...
.set({ brand })

But it seems really weird that typeorm won't let me set the id field directly. Also it shouldn't fail silently. Because of that I will let this issue open for further discussion.

@vlapo
Copy link
Contributor

vlapo commented Dec 22, 2018

I think you have defined brand relation in your entity and do not have brandId column defined as well. So typeorm escaped unknown columns.

Try add brandId column definition to your entity:

@Column()
brandId: number;

And it should worked as expected.

Also this should work:

.set({ brand: { id: brandId } })

@pleerock
Copy link
Member

yeah so we might need to report some error if user passed unknown properties in the object being passed within set method?

@vlapo
Copy link
Contributor

vlapo commented Dec 23, 2018

I think it should be good manner to throw error in case final map of values to update is empty. In UpdateQueryBuilder.getValueSet() is small empty check and throw. But later in createUpdateExpression() is this map filtered and possible ends with empty array of values to change.

Also It should be nice to allow update "columns generated by relations" as in this case where brandId is generated by ORM.

@pleerock
Copy link
Member

@vlapo I think we shall check both emptiness and wrong properties in.

@vlapo
Copy link
Contributor

vlapo commented Dec 24, 2018

Sure I agree. But what about join column not defined in entity as property like brandId in this issue?

Maybe we should allow update brandId even if it is not defined in entity.

Example:

@Entity()
export class Post {

    @PrimaryGeneratedColumn()
    id: number;

    @ManyToOne(type => Author, author => author.posts)
    @JoinColumn()
    author: Author;
}
const query = connection.manager
            .createQueryBuilder()
            .update(Post)
            .set({ authorId: 2 })
            .where({ id: 1 });
console.log(query.getSql());

This should be valid way to update author in post.

@pleerock
Copy link
Member

no, I don't think so. This design isn't clear. What is authorId in there? It means we would allow to specify any database column as well? Why only database column and property name? What about other expressions UPDATE SET supports?

In terms of this issue we only need to add those exceptions and close it. If we want to extend update query builder functionality, we need to create a separate issue and describe a new proposal of update query builder that covers all cases.

@vlapo
Copy link
Contributor

vlapo commented Dec 25, 2018

Understand. But still I would love to see this case Update relation only with "id" in some FAQ. I will think about it and try improve docs with PR. Just because this isn't first issue and question about this.

For this issue I will create PR and handle emptiness and wrong properties 👍

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

Successfully merging a pull request may close this issue.

4 participants