Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(postgres): removed erroneous duplicates in FK discovery query #5376

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions packages/knex/src/schema/SchemaHelper.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { BigIntType, EnumType, RawQueryFragment, Utils, type Connection, type Dictionary } from '@mikro-orm/core';
import type { Knex } from 'knex';
import { BigIntType, EnumType, Utils, type Connection, type Dictionary, RawQueryFragment } from '@mikro-orm/core';
import type { AbstractSqlConnection } from '../AbstractSqlConnection';
import type { AbstractSqlPlatform } from '../AbstractSqlPlatform';
import type { CheckDef, Column, IndexDef, Table, TableDifference } from '../typings';
import type { DatabaseTable } from './DatabaseTable';
import type { DatabaseSchema } from './DatabaseSchema';
import type { DatabaseTable } from './DatabaseTable';

export abstract class SchemaHelper {

constructor(protected readonly platform: AbstractSqlPlatform) {}
constructor(protected readonly platform: AbstractSqlPlatform) { }

getSchemaBeginning(charset: string): string {
return `${this.disableForeignKeysSQL()}\n\n`;
Expand Down Expand Up @@ -245,13 +245,8 @@ export abstract class SchemaHelper {
mapForeignKeys(fks: any[], tableName: string, schemaName?: string): Dictionary {
return fks.reduce((ret, fk: any) => {
if (ret[fk.constraint_name]) {
if (!ret[fk.constraint_name].columnNames.includes(fk.column_name)) {
ret[fk.constraint_name].columnNames.push(fk.column_name);
}

if (!ret[fk.constraint_name].referencedColumnNames.includes(fk.referenced_column_name)) {
ret[fk.constraint_name].referencedColumnNames.push(fk.referenced_column_name);
}
ret[fk.constraint_name].columnNames.push(fk.column_name);
ret[fk.constraint_name].referencedColumnNames.push(fk.referenced_column_name);
} else {
ret[fk.constraint_name] = {
columnNames: [fk.column_name],
Expand Down
10 changes: 5 additions & 5 deletions packages/postgresql/src/PostgreSqlSchemaHelper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BigIntType, type Dictionary, EnumType, Type, Utils } from '@mikro-orm/core';
import { BigIntType, EnumType, Type, Utils, type Dictionary } from '@mikro-orm/core';
import {
SchemaHelper,
type AbstractSqlConnection,
type CheckDef,
type Column,
Expand All @@ -8,7 +9,6 @@ import {
type ForeignKey,
type IndexDef,
type Knex,
SchemaHelper,
type Table,
type TableDifference,
} from '@mikro-orm/knex';
Expand Down Expand Up @@ -209,17 +209,17 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
async getAllForeignKeys(connection: AbstractSqlConnection, tables: Table[]): Promise<Dictionary<Dictionary<ForeignKey>>> {
const sql = `select nsp1.nspname schema_name, cls1.relname table_name, nsp2.nspname referenced_schema_name,
cls2.relname referenced_table_name, a.attname column_name, af.attname referenced_column_name, conname constraint_name,
confupdtype update_rule, confdeltype delete_rule,*
confupdtype update_rule, confdeltype delete_rule, array_position(con.conkey,a.attnum) as ord
from pg_attribute a
join pg_constraint con on con.conrelid = a.attrelid AND a.attnum = ANY (con.conkey)
join pg_attribute af on af.attnum = ANY (con.confkey) AND af.attrelid = con.confrelid
join pg_attribute af on af.attnum = con.confkey[array_position(con.conkey,a.attnum)] AND af.attrelid = con.confrelid
join pg_namespace nsp1 on nsp1.oid = con.connamespace
join pg_class cls1 on cls1.oid = con.conrelid
join pg_class cls2 on cls2.oid = confrelid
join pg_namespace nsp2 on nsp2.oid = cls2.relnamespace
where (${tables.map(t => `(cls1.relname = '${t.table_name}' and nsp1.nspname = '${t.schema_name}')`).join(' or ')})
and confrelid > 0
order by nsp1.nspname, cls1.relname, constraint_name`;
order by nsp1.nspname, cls1.relname, constraint_name, ord`;

const allFks = await connection.execute<any[]>(sql);
const ret = {} as Dictionary;
Expand Down
Loading