typeorm / typeorm

ORM for TypeScript and JavaScript. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms.
http://typeorm.io
MIT License
33.97k stars 6.26k forks source link

TypeORM 0.3.7 "where" property requires selection condition while it is provided #9208

Open bougwal opened 2 years ago

bougwal commented 2 years ago

Issue Description

TypeORM v 0.3.7 where property errors when used with GraphQL ^16.5.0 and MySQL ^2.18.1 requiring selection condition while it is provided.

Expected Behavior

    getUser: async (_:any, args:any, ctx: Context) => {
      const orm = ctx.orm;
      const user = await orm.userRepository.findOne({ where: { id: args.userId } });
      if (!user) {
        throw new ApolloError("No user found", "USER_NOT_FOUND");
      }
      return user as unknown as User;
    }

Actual Behavior

Compile time error: You must provide selection conditions in order to find a single row.

My Environment

Dependency Version
Operating System Windows
Node.js version v16.13.0
Typescript version ^4.7.4
TypeORM version ^0.3.7
GraphQL version ^16.5.0
ExpressJS version ^4.18.1

Additional Context

Relevant Database Driver(s)

DB Type Reproducible
mysql yes

Are you willing to resolve this issue by submitting a Pull Request?

Note: I downgraded to ^0.2.37 with the same resolver function above and I got the results I expect correctly

albjeremias commented 1 year ago

I'm having the same issue: const defaultNetwork = await this.networkRepository.findOne({order: {created_at: "ASC"}});

I get the error: [Nest] 304561 - 11/24/2022, 4:18:48 AM ERROR [ExceptionsHandler] You must provide selection conditions in order to find a single row.

albjeremias commented 1 year ago

any simple workaround? please? or i just have to downgrade typeorm?

oceanwap commented 1 year ago

@albjeremias A simple workaround would be to add below

      where: {
        id: Not(IsNull()),
      },

This condition would always be true on primary key.

although you have to add it in every findOne statements.

ArielPrevu3D commented 1 year ago

Is args.userId undefined or null, by chance?

Gauravdarkslayer commented 7 months ago

In my case it was wrong syntax, the correct syntax is const dummy = await this.dummyRepository.findOne({ where: { id } });

In previous versions it used to work without where key, but now it's required.

kkeolmusae commented 7 months ago

It also works like this.

const result = await this.repo.findOne({ where:{} })