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

repository.findOne(entity.constructor) returns an entity even if FindConditions don't exists #3455

Closed
carlosdubus opened this issue Jan 18, 2019 · 1 comment

Comments

@carlosdubus
Copy link

carlosdubus commented Jan 18, 2019

Issue type:

[x] question
[x] bug report

Database system/driver:

[x] mysql / mariadb

TypeORM version:

[x] latest

Steps to reproduce or a small repository showing the problem:

I know I should provide User class itself to findOne, but I found myself using obj.constructor to save an entity. Looks like TypeScript skips the type validation of FindConditions<T> because T is an {} I think. Nevertheless it shouldn't return an entity that does not match the FindConditions provided. Correct me if I'm wrong. Thanks!

import {Entity, PrimaryGeneratedColumn, Column, getManager, createConnection} from "typeorm";

@Entity()
export class User {
    @PrimaryGeneratedColumn()
    id: number;
    @Column()
    name: string;
}

async function main() {
    await createConnection(); // create connection somehow
    var search = {fieldNotExists: "1"}; // field does not exists on User class
    var user = new User();
    var r = await getRepository<User>(user.constructor).findOne(search);
    console.log(r); // returns first entity of table instead of undefined
}

main();

@carlosdubus carlosdubus changed the title getManager().findOne(entity.constructor) returns an entity even if FindConditions don't exists repository.findOne(entity.constructor) returns an entity even if FindConditions don't exists Jan 18, 2019
@vlapo
Copy link
Contributor

vlapo commented Jan 18, 2019

Duplicate of #3416. We just discuss about this problem. Unknown fields are stripped from query so result condition is {} and it is returned first entity in table.

@vlapo vlapo closed this as completed Jan 18, 2019
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