Skip to content

Table name on pivot tables #2919

@B4nan

Description

@B4nan

Test case:

create schema accounting;

create table accounting.account(
	id bigserial primary key,
	iban text not null
);

create table accounting.customer(
	id bigserial primary key,
	name text not null
);

create table accounting.company(
	id bigserial primary key,
	name text not null
);

create table accounting.customer_account(
	id bigserial primary key,
	customer_id bigint references accounting.customer on update cascade on delete cascade,
	account_id bigint references accounting.account on update cascade on delete cascade
);

create table accounting.company_account(
	id bigserial primary key,
	company_id bigint references accounting.company on update cascade on delete cascade,
	account_id bigint references accounting.account on update cascade on delete cascade
);
(async () => {

    @Entity({
        schema: 'accounting',
        tableName: 'account'
    })
    class Account {
        constructor(iban: string) {
            this.iban = iban;
        }

        @PrimaryKey({
            nullable: true
        })
        id: string;

        @Property()
        iban!: string;

        @ManyToMany({ entity: () => Customer, mappedBy: (c: Customer) => c.accounts })
        customers: Collection<Customer> = new Collection<Customer>(this);

        @ManyToMany({ entity: () => Company, mappedBy: (c: Company) => c.accounts })
        companies: Collection<Company> = new Collection<Company>(this);
    }

    @Entity({
        schema: 'accounting',
        tableName: 'customer'
    })
    class Customer {
        @PrimaryKey({
            nullable: true
        })
        id: string;

        @Property()
        name!: string;

        @ManyToMany({
            entity: () => Account,
            pivotTable: 'accounting.customer_account',
            joinColumn: 'customer_id',
            inverseJoinColumn: 'account_id'
        })
        accounts: Collection<Account> = new Collection<Account>(this)
    }

    @Entity({
        schema: 'accounting',
        tableName: 'company'
    })
    class Company {
        @PrimaryKey({
            nullable: true
        })
        id: string;

        @Property()
        name!: string;

        @ManyToMany({
            entity: () => Account,
            pivotTable: 'accounting.company_account',
            joinColumn: 'company_id',
            inverseJoinColumn: 'account_id'
        })
        accounts: Collection<Account> = new Collection<Account>(this);
    }

    let orm: MikroORM<PostgreSqlDriver>;
    try {
        orm = await MikroORM.init({
            entities: [Account, Customer, Company],
            dbName: 'mikro-test',
            type: 'postgresql',
            host: '192.168.2.12',
            port: 5432,
            user: 'postgres',
            password: 'whatever',
            debug: true,
            metadataProvider: ReflectMetadataProvider
        });

        const em = orm.em.fork();

        const customer = new Customer();
        customer.name = 'Customer 1';
        customer.accounts.add(new Account('AAA-AAA-AAA'), new Account('BBB-BBB-BBB'));
        em.persist(customer);

        const company = new Company();
        company.name = 'Company 1';
        company.accounts.add(new Account('CCC-CCC-CCC'));
        em.persist(company);

        await em.commit();

        // I want to find all accounts from company 1
        const em2 = orm.em.fork();

        const companyAccounts = await em2.find(
            Account,
            {
                companies: {name: 'Company 1'}
            }
        );
        console.log(companyAccounts);
    } catch (e) {
        console.error(e);
    } finally {
        await orm.close();
    }
})();

Throws an error (as explained):

[query] select "a0".* from "accounting"."account" as "a0" left join "accounting"."accounting"."company_account" as "a2" on "a0"."id" = "a2"."account_id" left join "accounting"."company" as "a1" on "a2"."company_id" = "a1"."id" where "a1"."name" = 'Company 1' [took 2 ms]

Notice the double schema on pivot table

Originally posted by @jeroni in #2918 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions