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

[BUG]: $inferInsert and $inferSelect are not composable #3528

Open
1 task done
tmeisenh opened this issue Nov 11, 2024 · 0 comments
Open
1 task done

[BUG]: $inferInsert and $inferSelect are not composable #3528

tmeisenh opened this issue Nov 11, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@tmeisenh
Copy link

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?

0.36.1

What version of drizzle-kit are you using?

0.28.0

Other packages

No response

Describe the Bug

Using database first, I have an existing table that looks like this

CREATE TABLE example(
    	id integer primary key GENERATED BY DEFAULT AS IDENTITY,
	valid_to timestamptz,
	created timestamptz NOT NULL,
	updated timestamptz NOT NULL
);

The drizzle-kit generated schema might look like this

  {
    id: integer().generatedByDefaultAsIdentity({
      name: 'example_id_seq',
      startWith: 1,
      increment: 1,
      minValue: 1,
      maxValue: 2147483647,
      cache: 1,
    }),
    validTo: timestamp('valid_to', { withTimezone: true, mode: 'date' }),
    created: timestamp({ withTimezone: true, mode: 'date' }).notNull(),
    updated: timestamp({ withTimezone: true, mode: 'date' }).notNull(),
  },

The $inferInsert type will evaluate to

export interface ExampleInferInsert  {
	validTo: Date | null | undefined;
	created: Date;
	updated: Date;
}

The $inferSelect type will evaluate to

export interface ExampleInferSelect {
	id: number;
	validTo: Date | null;
	created: Date;
	updated: Date;
}

As you can see, the optional/nullable fields have different types. This means I can't use the inferInsert data to compose an inferSelect data like I want to do in my unit/e2e tests.

const inputData: ExampleInferInsert = {
validTo: null,
created: new Date(),
updated: new Date()
};


// complains about validTo's type from ExampleInferInsert not being compatible with ExampleInferSelect's validTo type
const expected: ExampleInferSelect = {
	...inputData,
	id: expect.any(Number),
}

// complains about validTo's type from ExampleInferInsert not being compatible with ExampleInferSelect's validTo type
const expected0: ExampleInferSelect = {
	...inputData,
	id: 0,
}

const actual = await dao.save(inputData);
expect(actual).toEqual(expected);

My expectation is that I could use the $inferInsert to compose a $selectInput. Maybe there is a way for the $inferInsert to not have it be undefined and just be or null?

@tmeisenh tmeisenh added the bug Something isn't working label Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant