Skip to content

Upsert ChildEntity cause QueryFailedError: missing FROM-clause entry for table "xxx" in PostgreSQL schema #11689

Closed

Description

@pilaoda

Issue description

This PR #10974 introduce a bug in PostgreSQL with schema option if upsert a ChildEntity.

Expected Behavior

We should escape schema.table.column here to "schema"."table"."column".

const column = this.expressionMap.aliasNamePrefixingEnabled
? this.expressionMap.mainAlias!.name +
"." +
metadata.deleteDateColumn.propertyName
: metadata.deleteDateColumn.propertyName

  • Expect correct sql:
INSERT INTO "sc"."parent"("id", "name", "type", "key") VALUES ($1, $2, $3, $4) ON CONFLICT ( "id" ) DO UPDATE 
SET "key" = EXCLUDED."key" 
WHERE ( "sc"."parent"."key" IS DISTINCT FROM EXCLUDED."key" ) 
AND ( "sc"."parent"."type" IN ($5) ) [ 1, 'name1', 'Child1', 'key1', 'Child1' ]

Actual Behavior

INSERT INTO "sc"."parent"("id", "name", "type", "key") VALUES ($1, $2, $3, $4) ON CONFLICT ( "id" ) DO UPDATE 
SET "key" = EXCLUDED."key" 
WHERE ( "sc"."parent"."key" IS DISTINCT FROM EXCLUDED."key" ) 
AND ( "sc.parent"."type" IN ($5) ) [ 1, 'name1', 'Child1', 'key1', 'Child1' ]

it throw: QueryFailedError: missing FROM-clause entry for table "sc.parent"

Note: "sc.parent" after AND should be "sc"."parent"

Steps to reproduce

  • My full test code
import { PostgreSqlContainer } from "@testcontainers/postgresql";
import { type StartedTestContainer } from "testcontainers";
import { ChildEntity, Column, DataSource, Entity, PrimaryColumn, PrimaryGeneratedColumn, TableInheritance, UpdateDateColumn, VersionColumn } from "typeorm";


let container: StartedTestContainer;
let connection: DataSource;

const PGDATABASE = "test";
const PGUSER = "test";
const PGPASSWORD = "test";
const PGPORT = 5432;
const SCHEMA = "sc";
beforeAll(async () => {
	container = await new PostgreSqlContainer("postgres:16")
		.withExposedPorts(PGPORT)
		.withDatabase(PGDATABASE)
		.withUsername(PGUSER)
		.withPassword(PGPASSWORD)
		.start();

	const prepare = new DataSource({
		type: "postgres",
		host: "localhost",
		port: container.getMappedPort(PGPORT),
		username: PGUSER,
		password: PGPASSWORD,
		database: PGDATABASE,
	});
	await prepare.initialize();
	await prepare.query(`CREATE SCHEMA IF NOT EXISTS ${SCHEMA}`);
	await prepare.destroy();

	connection = new DataSource({
		type: "postgres",
		host: "localhost",
		port: container.getMappedPort(PGPORT),
		username: PGUSER,
		password: PGPASSWORD,
		database: PGDATABASE,
		synchronize: true,
		schema: SCHEMA,
		entities: [Parent, Child1],
	});
	await connection.initialize();
}, 0);

afterAll(async () => {
	await connection.destroy();
});

@Entity()
@TableInheritance({ column: { type: "varchar", name: "type" } })
class Parent {
	@PrimaryColumn()
	id!: number;

	@Column()
	name!: string;

	@Column()
	type!: string;
}

@ChildEntity()
class Child1 extends Parent {
	@Column({ type: "text" })
	key!: string;
}


it("upsert Child1", async () => {
	const queryBuilder = connection.createQueryBuilder()
		.insert()
		.into(Child1)
		.values([{ id: 1, name: "name1", key: "key1", type: "Child1" }])
		.orUpdate(["key"], ["id"], { skipUpdateIfNoValuesChanged: true });
	const [sql, parameters] = queryBuilder.getQueryAndParameters();
	console.log(sql, parameters);
	const result = await connection
		.createQueryRunner()
		.query(sql, parameters);
	expect(result).toBeDefined();
});

My Environment

Dependency Version
Operating System Windows11
Node.js version 22.14.0
Typescript version 5.8.2
TypeORM version 0.3.27

Additional Context

Sorry, I'm not familiar with the testing framework of TypeORM, so I started with using testcontainers for testing. However, I hope I have provided sufficient information that test code can be easily copy into TypeORM's testing framework.

Relevant Database Driver(s)

  • aurora-mysql
  • aurora-postgres
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • spanner
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

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

No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions