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

findOneOrFail(null) or findOneOrFail(undefined) should throw error #4373

Closed
lleohao opened this issue Jun 29, 2019 · 9 comments
Closed

findOneOrFail(null) or findOneOrFail(undefined) should throw error #4373

lleohao opened this issue Jun 29, 2019 · 9 comments

Comments

@lleohao
Copy link

lleohao commented Jun 29, 2019

Issue type:

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

Database system/driver:

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

TypeORM version:

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

Steps to reproduce or a small repository showing the problem:

// category entity
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class CategoryEntity {
    @PrimaryGeneratedColumn()
    id: number;

    @Column({
        unique: true,
    })
    name: string;
}

// create one category
const  categryEntity = new CategoryEntity()
categryEntity.name = 'first';

await getRepository(CategoryEntity).save(categryEntity);

// findOneOrFail
const result = getRepository(CategoryEntity).findOneOrFail(null);
// expect throw error but result.toEqual(categryEntity)

@likyh
Copy link

likyh commented Jul 21, 2019

+1

@jelling
Copy link

jelling commented Aug 6, 2019

This needs to be fixed asap imo. If a null value is passed into a query TypeORM is retrieving a random record. This could cause serious data issues for someone.

@btorresbrandcoders
Copy link

Can verify that the same issue is occurring with PostgreSQL as well. This is a serious bug and should be getting more attention. The fact that a random record is returned is very concerning.

@dtmzr
Copy link

dtmzr commented Aug 22, 2019

I recognized the same behavior on postgres.
I passed a parameter id straight to the method, whenever the id was undefined I had the same behavior.
However, passing an object works.

// This doesn't throw
function getCategoryById(id) {
  return getRepository(CategoryEntity).findOneOrFail(id);
}
// This throws
function getCategoryById(id) {
  return getRepository(CategoryEntity).findOneOrFail({ id });
}

I think this is related to the method overloading here.

@mgibson91
Copy link

mgibson91 commented Oct 16, 2019

As @dtmzr mentioned, I experienced the first record always getting returned when where conditions weren't met using the following syntax:

Entity.findOneOrFail({ where: { id: userId } }) (expected this to work from other typeorm experience.

It did however, work after removing the where:

Entity.findOneOrFail({ id: userId })

Confusing overloading

@imnotjames
Copy link
Contributor

duplicate of 2990

@dtmzr
Copy link

dtmzr commented Oct 7, 2020

Hey, @imnotjames the # was missing and therefore was not referenced.

@imnotjames
Copy link
Contributor

Oops

duplicate of #2990

@apflieger
Copy link

I just upgraded to 0.2.29 but it still happen 😢

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

8 participants