Duplicate aliases

Hello,

In a table, I have 2 columns which are foreign keys to same table, and generated schema create error at runtime :

warning: Skipping relation “partner.partners_through_partner_contract_templates” because of error: You have used the alias partners_through_partner_contract_templates in two separate associations. Aliased associations must have unique aliases.

How can I fix this ?

Regards,

David

Hello @dscreve

Which datasource are you using ?

createSqlDataSource and createSequelizeDataSource

Thank you.

The error seems to be related to Sequelize, I think you have the same alias on 2 relations.

Modifying one of the aliases might fix your issue.

Actually I have not modified anything…this is an auto-generated alias.
How can I change it ? (I’m not expert of Sequelize indeed).

Hello @dscreve

What generated the sequelize models ?

Is partner one of the models you use with the sequelize datasource ?

I don’t have written anything for those tables :

CREATE TABLE IF NOT EXISTS public.partner
(
    id BIGSERIAL NOT NULL,
    external_id TEXT NOT NULL,
    code text NOT NULL,
    label text NOT NULL,
    customer_service_email TEXT,
);

CREATE TABLE IF NOT EXISTS public.partner_contract_templates (
	warranty_partner_id BIGINT NOT NULL,
	lender_partner_id  BIGINT NOT NULL,
	pledge_template_res_path TEXT
);

But it seems the alias is generated in .forestadmin.schema.json…I don’t know how to customise this.

David

Hello @dscreve,

Could you please share the relations you have defined on your partner_contract_templates table ?
My first guess is that the constraints you have defined to the partner table have duplicated names which causes errors on introspection.

Best regards,

Here there are :

ALTER TABLE public.partner_contract_templates ADD CONSTRAINT pk_partner_contract_templates PRIMARY KEY(warranty_partner_id,lender_partner_id);

ALTER TABLE ONLY public.partner_contract_templates ADD CONSTRAINT fk_partner_contract_templates_warranty_partner_id
     FOREIGN KEY (warranty_partner_id) REFERENCES public.partner(id) ON DELETE CASCADE;

ALTER TABLE ONLY public.partner_contract_templates ADD CONSTRAINT fk_partner_contract_templates_lender_partner_id
     FOREIGN KEY (lender_partner_id) REFERENCES public.partner(id) ON DELETE CASCADE;

Regards,

I tried to replicate your setup via the queries you provided, I have no issue making it work with the createSqlDataSource, on which version of this package do you experience this issue ?

I am also wondering why and how you are using 2 different dataSources

I don’t know…Some parts of this code has been written by someone else…I think my colleague wrote this code with some people from the support.

Actually, some views have Sequelize customizations, that maybe the reason Sequelize is also used.

Here are the versions I’m using :

@forestadmin/agent”: “^1.35.17”,
@forestadmin/datasource-sequelize”: “^1.5.24”,
@forestadmin/datasource-sql”: “^1.7.39”,

I still cannot reproduce with datasource-sql: ^1.7.39
Let’s try to single out the datasource causing this issue in your configuration, could you please start the agent with one datasource at a time ?

If I keep only one datasource, some collections are missing…

I am sorry if It was not clear, I am suggesting you to start your agent on your development environment with only one datasource at a time to check which one logs the warning Skipping relation “partner.partners_through_partner_contract_templates”. This is only temporary so as to not investigate on the wrong datasource.
As the sqlDataSource does the introspection on its own and the sequelizeDataSource works based on the sequelize instance with loaded models (and their configuration) that you pass as parameter.

If you are unable to start your agent with only one datasource because of customizations that require collections from both datasources, we can simply check any customizations or models defined for your table partner_contract_templates

I have removed collection customization to be able to run it…Problem does not occurs If I use createSequelizeDataSource, but it still remains if I use createSqlDataSource.

Regards,

Hello @dscreve,

Thanks for the additional information.
Sadly I still couldn’t reproduce the issue, if you are comfortable with it, could you please send me a backup of your database only containing the structure, no data, via private message.

I cannot do that…I can pass some command for you, but I can’t send entire database structure.

I understand, then if you could provide as many information on your partner and partner_contract_templates tables that would help greatly.

As well as the database you are using and its version.

I found the problem and fixed it…

I had the following tables :

CREATE TABLE IF NOT EXISTS public.partner
(
id BIGSERIAL NOT NULL,
external_id TEXT NOT NULL,
code text NOT NULL,
label text NOT NULL,
);

CREATE TABLE IF NOT EXISTS public.partner_contract_templates (
warranty_partner_id BIGINT NOT NULL,
lender_partner_id BIGINT NOT NULL,
);

CREATE TABLE IF NOT EXISTS public.acceptable_lender_warranty_partnership (
warranty_partner_id BIGINT NOT NULL,
lender_partner_id BIGINT NOT NULL
);

public.partner_contract_templates and public.acceptable_lender_warranty_partnership have foreign keys to partner table with same field name. This was causing trouble when Sequelize generate reverse relation.

I have renamed fields like below and it works fine now :
CREATE TABLE IF NOT EXISTS public.acceptable_lender_warranty_partnership (
acceptable_warranty_partner_id BIGINT NOT NULL,
acceptable_lender_partner_id BIGINT NOT NULL
);

Anyway, thanks for your help,

David

2 Likes