typeorm: missing FROM-clause entry for table "???????"

Issue type:

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

Database system/driver:

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

TypeORM version:

[ ] latest [ ] @next [X] 0.x.x (0.2.7)

Steps to reproduce or a small repository showing the problem: My test in pgAdmin4 SELECT “campaignId”, COUNT (“campaignId”) AS “totalCampaign” FROM public.??? WHERE “campaignId” = ANY (ARRAY [31,30,28,29,23,19,24,20,21,22]) GROUP BY “campaignId” it’s OK

const countContact = await this.???Repository.createQueryBuilder(‘???’) .select(‘???.campaignId’) .addSelect(‘COUNT(???.campaignId)’, ‘totalCampaign’) .where(‘???.campaingId = ANY(:data)’, {data}) .groupBy(‘???.campaignId’) .execute();

The query is wrong, Thank

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 4
  • Comments: 15 (2 by maintainers)

Most upvoted comments

Same issue:

SELECT
    "organization"."id" AS "organization_id",
    "organization"."active" AS "organization_active",
    "organization"."createdAt" AS "organization_createdAt",
    "organization"."createdById" AS "organization_createdById",
    "organization"."createdByOrganizationId" AS "organization_createdByOrganizationId",
    "organization"."updatedAt" AS "organization_updatedAt",
    "organization"."updatedById" AS "organization_updatedById",
    "organization"."updatedByOrganizationId" AS "organization_updatedByOrganizationId",
    "organization"."key" AS "organization_key",
    "organization"."name" AS "organization_name",
    "organization"."isAdmin" AS "organization_isAdmin"
FROM
    "organization" "organization"
    INNER JOIN "user" "User" ON "User"."organizationId" = organization.organizationId
WHERE
    (
        "User"."organizationId" = $ 1
        AND "User"."id" = $ 2
    )

As you can see, I have defined a @JoinColumn but it’s not getting respected. And the generated query’s inner join doesn’t have double quotes in it.

Here is my entity definition:


@ObjectType({ isAbstract: true })
export class Node {
  @Field(type => ID)
  @PrimaryGeneratedColumn('uuid')
  id: string

  ...
}

@Entity()
@ObjectType()
export class Organization extends Node {
  ...
}

export class MultiTenantNode {
  @PrimaryColumn('uuid')
  organizationId: string

  @ManyToOne(type => Organization, { lazy: true })
  @JoinColumn({ name: 'organizationId', referencedColumnName: 'id' })
  organization: Lazy<Organization>

  @Field(type => ID)
  @PrimaryGeneratedColumn('uuid')
  id: string
}

@Entity()
@ObjectType()
export class User extends MultiTenantNode {
  ...
}

Hello Everyone.

@pleerock @Kononnable I think this could be possibly reopened.

I started facing the same issue today with TypeORM 0.2.24 using PostgreSQL driver when trying to fetch entities with @ManyToMany relation.

I don’t know why, but the generated query is missing the double quotes which seems to be the reason of missing FROM-clause entry for table errors.

Here is a simplified extract from my entities:

@Entity()
export class ProductOption {

    @ManyToMany(() => OptionValue, (value: OptionValue) => value.options)
    @JoinTable({ name: 'product_option_value' })
    public values: OptionValue[];
}

@Entity()
export class OptionValue {

    @Column({
        type: 'simple-json',
    })
    public value: { baseValue: string, alternativeValue: string }; // just an example

    @ManyToMany(() => ProductOption, (productOption: ProductOption) => productOption.values)
    public options: ProductOption[];
}

See I have both sides of ManyToMany relation defined with a custom-named @JoinTable.

Now, when I try to fetch Options with related OptionValues, e.g.

productOptionRepository.find({ relations: ["values"] });

I get this query generated and this FAILS:

query failed: SELECT ProductOption_values_rid.optionValueId AS “optionValueId”, ProductOption_values_rid.productOptionId AS “productOptionId” FROM “option_value” “option_value” INNER JOIN “product_option_value” “ProductOption_values_rid” ON (ProductOption_values_rid.productOptionId = $1 AND ProductOption_values_rid.optionValueId = “option_value”.“id”) ORDER BY ProductOption_values_rid.optionValueId ASC, ProductOption_values_rid.productOptionId ASC – PARAMETERS: [123]

error: missing FROM-clause entry for table “productoption_values_rid”

Please note the bold parts - they are missing double quotes. I compared that to a different but similar relation in other part of my apps, and there the query includes double quotes! This is what I found in different sources can cause such missing FROM-clause entry for table errors.

@ishan123456789 provided me a hint to check if the simple-json may be causing this issue. So I removed the relation:

productOptionRepository.find();

and voliá! It magically works. There has to be something wrong with the query and/or pivot table being generated automatically by TypeORM for @ManyToMany relation when we have an entity containing simple-json column.

Any help kindly appreciated!

I ma facing same issue, when trying to save entity with many2many relationship. This is the query its generating: SELECT Campaign_businessGroups_rid.Business_Group_Id AS "Business_Group_Id", Campaign_businessGroups_rid.Campaign_Id AS "Campaign_Id" FROM "Business_Group" "Business_Group" INNER JOIN "Campaign_Business_Group" "Campaign_businessGroups_rid" ON (Campaign_businessGroups_rid.Campaign_Id = $1 AND Campaign_businessGroups_rid.Business_Group_Id = "Business_Group"."Business_Group_Id") ORDER BY Campaign_businessGroups_rid.Business_Group_Id ASC, Campaign_businessGroups_rid.Campaign_Id ASC'

This is the error I am getting: QueryFailedError: missing FROM-clause entry for table "campaign_businessgroups_rid"

You can follow my solution if another do not work. Just change your table name from Snake case to Camel case https://github.com/typeorm/typeorm/issues/3521#issuecomment-767252311

Same problem here, it was resolved by using alias as the name of the table that i want to join with…!

I ma facing same issue, when trying to save entity with many2many relationship. This is the query its generating: SELECT Campaign_businessGroups_rid.Business_Group_Id AS "Business_Group_Id", Campaign_businessGroups_rid.Campaign_Id AS "Campaign_Id" FROM "Business_Group" "Business_Group" INNER JOIN "Campaign_Business_Group" "Campaign_businessGroups_rid" ON (Campaign_businessGroups_rid.Campaign_Id = $1 AND Campaign_businessGroups_rid.Business_Group_Id = "Business_Group"."Business_Group_Id") ORDER BY Campaign_businessGroups_rid.Business_Group_Id ASC, Campaign_businessGroups_rid.Campaign_Id ASC'

This is the error I am getting: QueryFailedError: missing FROM-clause entry for table "campaign_businessgroups_rid"

+1