0

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?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.