Skip to content

[BUG]: drizzle-kit pull generates not enough data provided to build the relation #5493

@cong-tn

Description

@cong-tn

Report hasn't been filed before.

  • I have verified that the bug I'm about to report hasn't been filed before.

What version of drizzle-orm are you using?

1.0.0-beta.17-67b1795

What version of drizzle-kit are you using?

1.0.0-beta.17-67b1795

Other packages

No response

Describe the Bug

When I run the server with the relation generated by drizzle-kit pull, I encounter the following error.

server/node_modules/src/relations.ts:192
                                        throw new Error(
                                        ^


Error: relations -> customer: { billingAccountsViaContract: r.many.billingAccount(...) }: not enough data provided to build the relation - "from"/"to" are not defined, and there is no reverse relation of table "billingAccount" with alias "billingAccount_customerId_customer_customer_id_via_contract"

The file relations.ts generated is as follows:

export const relations = defineRelations(schema, (r) => ({
	billingAccount: {
		customer: r.one.customer({
			from: r.billingAccount.customerId,
			to: r.customer.customerId,
			alias: "billingAccount_customerId_customer_customerId"
		}),
		customers: r.many.customer({
			from: r.billingAccount.billingAccountId.through(r.contract.billingAccountId),
			to: r.customer.customerId.through(r.contract.customerId),
			alias: "billingAccount_billingAccountId_customer_customerId_via_contract"
		}),
	},
	customer: {
		billingAccountsCustomerId: r.many.billingAccount({
			alias: "billingAccount_customerId_customer_customerId"
		}),
		billingAccountsViaContract: r.many.billingAccount({
			alias: "billingAccount_customerId_customer_customer_id_via_contract"
		}),
	},
}))

Reproduce steps

  1. Create a PostgreSQL database using the SQL below.
CREATE TABLE customer (
	customer_id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY	
);

CREATE TABLE billing_account (
	billing_account_id int8 GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
	customer_id int8 NOT NULL
);
ALTER TABLE billing_account ADD CONSTRAINT billing_account_customer_id_fkey FOREIGN KEY (customer_id) REFERENCES public.customer(customer_id);


CREATE TABLE contract (
	contract_id int8 GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
	customer_id int8 NOT NULL,
	billing_account_id int8 NOT NULL
);
ALTER TABLE contract ADD CONSTRAINT contract_billing_account_id_fkey FOREIGN KEY (billing_account_id) REFERENCES billing_account(billing_account_id);
ALTER TABLE contract ADD CONSTRAINT contract_customer_id_fkey FOREIGN KEY (customer_id) REFERENCES customer(customer_id);
  1. Run drizzle-kit pull

I want drizzle-kit to generate a relations.ts file with enough data for all relations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    1.0.0-beta.*bugSomething isn't workingbug/fixed-in-betaThis bug has been fixed in beta (or will be soon).

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions