Skip to content

Ambiguous column when joining table using createQuery #3585

@tomek1972

Description

@tomek1972

In my database all tables have key Id. How should I join tables correctly?

For example, right now I'm trying to join the Users and Companies tables like this:

export class UserService<ServiceParams extends Params = UserParams> extends KnexService<User, UserData, UserParams, UserPatch> {
  createQuery(params: UserParams) {
    const query = super.createQuery(params)

    query
      .leftJoin('Companies', 'Users.CompanyId', 'Companies.Id')
      .select('Companies.Name as CompanyName')
    return query
  }
}

Unfortunately I get an error message:
select [Users].*, [Companies].[Name] as [CompanyName] from [Users] left join [Companies] on [Users].[CompanyId] = [Companies].[Id] where [Id] = @p0 and [Users].[Id] = @p1 - Ambiguous column name 'Id'.

In the where section it is '[Id] = ' instead of '[Users].[Id] = '. What am I doing wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions