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

Surprising behavior of where with undeclared/mistyped keys #5579

Closed
subvertallchris opened this issue Feb 27, 2020 · 1 comment
Closed

Surprising behavior of where with undeclared/mistyped keys #5579

subvertallchris opened this issue Feb 27, 2020 · 1 comment

Comments

@subvertallchris
Copy link

Issue type:

[x] feature request
[x] bug report?

Database system/driver:

[x] postgres

TypeORM version:

[x] 0.2.18 (or put your version here)

Steps to reproduce or a small repository showing the problem:

Not sure if this is a feature request or a bug: I find the current behavior of where queries that reference undeclared properties very surprising.

Given a simple model:

@Entity()
class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  // @Column()
  // age: number;
}

Note that age is commented out. The column exists in my database but it is not declared in my entity.

Now given this query:

const user = await getRepository(User).find({ where: { name: 'Chris', age: 35 }});

I am seeking all users named Chris, age 35. In reality, I will get all users named Chris, age is ignored. I'd expect one of two things to happen:

  • It should either perform the query with my age property by passing it directly into SQL (SELECT * FROM users WHERE name = 'Chris' AND age = 35, essentially)...
  • ...or it should throw an error because I am using an undeclared property. The compiler should be aware that this field doesn't exist and could give me an error before I even ran the query.

In reality, it ignores keys that it doesn't recognize. The query that I think I'm making is not the one that is actually being made and nobody will catch it until a user or a test notices. This strikes me as somewhere between bug and feature request: it doesn't seem like a deliberate design decision but I'm not sure I'd call it broken, it strikes me as unimplemented behavior.

Part of the problem seems to be the types associated with where. Looking at the FindOneOptions interface, I see where defined:

where?: FindConditions<Entity>[] | FindConditions<Entity> | ObjectLiteral | string;

I can get a compiler error if I explicitly define the argument ahead of time:

// This will upset the compiler if there is an undefined property!
const where: FindConditions = { name: 'Chris', age: 35 };

But it seems to me that there's no point in where?: including FindOptions<Entity> since it will never infer that type, it will always infer the more permissive ObjectLiteral, which is basically any, but only as a way of ignoring things it doesn't recognize -- it'll just filter them out:

export interface ObjectLiteral {
    [key: string]: any;
}

This has bit me a few times and it's always surprising. Not only will it bite you if you're working with an undeclared property, it also won't prevent you from making typos in your queries, which is part of why I'm using TypeORM in the first place! I'm not sure what my recommendation here is but at the very least, I'd expect TypeORM to do something visible when it sees a property it doesn't recognize, not just silently eat keys.

Thanks for your help. I love the work you guys are doing, TypeORM is a fantastic project.

@imnotjames
Copy link
Contributor

Duplicate of #3416

@imnotjames imnotjames marked this as a duplicate of #3416 Oct 12, 2020
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

2 participants