typeorm: TypeError: Cannot read property 'databaseName' of undefined

Issue type: [x] bug report

Database system/driver: [x] mssql

TypeORM version: [x] latest

Steps to reproduce or a small repository showing the problem:

Here is a repo with the bug: https://github.com/victorschinaider/typeorm-bug

I think pagination+ embedded orderBy+ subquery + leftJoinAndSelect combination is giving me the problem.

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 25
  • Comments: 39 (4 by maintainers)

Commits related to this issue

Most upvoted comments

I have the same issues. As for me, it helps

@Entity({name: 'categories'})
export class Category extends BaseEntity {
    @PrimaryGeneratedColumn()
    public id: number;

    @Column({name: 'my_name'})
    public name: string;
}

In QueryBuilder use qb.orderBy('categories.name', filter.orderDirection); Instead of column name from database. qb.orderBy('categories.my_name', filter.orderDirection);

So, I just replaced take with limit and it works for me.

#747 might help. OrderBy expects propertyNames instead of column names.

Waiting for a fix here.

if join、orderBy、take、skip are used together, the problem that Cannot read property 'databaseName' of undefined would happen.

there are two solutions. one could be found in the comment of this issue: https://github.com/typeorm/typeorm/issues/4270#issuecomment-987799139

anther solution as follows: use camel-case type instead of snake-case type in orderBy. for example, made a page query using orderBy of a.create_time which defined in the table, the orderBy here would be replaced with .orderBy('a.createTime', 'DESC').

Getting the same error while trying this combination with Postgres behind.

Cannot read property ‘databaseName’ of undefined

repository
.createQueryBuilder('invite')
.innerJoinAndSelect('invite.user', 'user')
.where('invite.user_id = :userId', { userId })
.orderBy('invite.updated_at', 'DESC', 'NULLS LAST')
.orderBy('invite.created_at', 'DESC')
.take(10)
.skip((page - 1) * 10)
.getMany();

Can try raw query for now.

Any idea on when/how this will be fixed? I’m also trying to orderBy on a leftJoinAndSelect column.

#747 might help. OrderBy expects propertyNames instead of column names.

Wtf?

I just spent hours trying to debug this. For me, makes no sense.

I got the same error when there were two columns listed in the orderBy - which works fine when used with limit, but fails when used with take.

.orderBy("colA, colB", "ASC")
.take(10)

Removing one of the columns made it work.

I agree, this is a very useless (and incorrect) error message. I also find the pagination documentation lacking, as it says:

take and skip may look like we are using limit and offset, but they aren’t. limit and offset may not work as you expect once you have more complicated queries with joins or subqueries. Using take and skip will prevent those issues.

but it does not elaborate on what these functions are doing that’s so special.

    const request = this.entityManager
      .createQueryBuilder(User, 'u')
      .where('u.user_uuid !=:user_uuid', { user_uuid: user.uuid })
      .innerJoinAndSelect('u.account', 'account', 'account.acco_uuid=:accountUuid', { accountUuid })
      .limit(take)
      .offset(skip)

    if (role) request.andWhere('u.role=:role', { role });
    if (by) request.orderBy(`u.${by}`, orderType === 'asc' ? 'ASC' : 'DESC');
    
    return request.getManyAndCount();

Also had this error . My solution set take, skip to limit, offset

Same issue, but none of the above solutions worked for me. I am using a JSON field to order by:

query.orderBy( 'call.data->>date', data.order);

The call.data is the property name, so it should work.

if I use the query from query.getQuery() in a PostgreSQL console the query works fine.

@wladimirguerra I think this should work:

query.addSelect('call.data->>date', 'callDataDate')
.orderBy('callDataDate', data.order)

This is a terrible error message. For me the issue was that the sort/orderby column was incorrect (didn’t exist - copy/pasta).

Check if your query params are correct. Here my ordering query was with an incorrect property name in sort and then this error was reported.

Fixing it, all works properly:

Captura de tela de 2020-06-26 01-04-30

Not supporting pagination is a bit of a deal breaker tbh. I like Typeorm but lacking real pagination is a pretty big issue. Been trying to fix this for a while now, to no avail

It’s not great when a single method change, in my case limit() -> take(), can blow up your entire app in such a misleading way…

But I can confirm using entity property names instead of column names in the orderBy clause in a query with a join worked.

I found that replacing take(1).getOne() with getOne() fixed this (and probably I should have made that change anyway).

I think you are using getMany() method of typeorm. You shouldn’t use like this orderBy(‘post.category_id’). You shuold use like this orderBy(‘post.category’)

#747 might help. OrderBy expects propertyNames instead of column names.

Is this in the docs ? I did’nt find this anywhere. I’ve lost good chunk of hours until I found your answer

To explain why is this important:

  • I have a second database, not owned by me, which is updated by an automated software ( PACS - medical system ). I have mapped the properties that I use into a local model, so I can take advantage of typeorm, but when I used the query builder, since I changed the aliases of column names on my side, I was not able to order the query. If this was in docs, it would be great!

I can make a pull request for this, since I’ll forget by the end of the week anyways

Now, my query is working as intended