[BUG]: type obtained with $inferInsert is empty
What version of drizzle-orm are you using?
0.32.0
What version of drizzle-kit are you using?
0.23.0
Describe the Bug
I defined a users table in my drizzle schema, and the NewUser type obtained with users.$inferInsert is empty:
Expected behavior
NewUser type should have most of the users table properties.
Environment & setup
Fedora Linux node-postgres typescript: 5.5.3
I encountered the same issue in drizzle-orm version 0.32.0, which was resolved after downgrading to version 0.31.4
Could you please send a schema as code snippet, so I can test with it?
Could you please send a schema as code snippet, so I can test with it?
import {type InferInsertModel} from 'drizzle-orm';
import {pgTable} from 'drizzle-orm/pg-core';
const Users = pgTable('users', {
id: serial('id').primaryKey(),
firstName: text('first_name'),
lastName: text('last_name'),
email: text('email'),
});
type NewUserType = InferInsertModel<typeof Users>;
@AndriiSherman the type of NewUserType would be an empty object
Could you please send a schema as code snippet, so I can test with it?
@AndriiSherman
import {
serial,
text,
timestamp,
pgTable,
boolean,
pgEnum,
} from 'drizzle-orm/pg-core';
export const roleEnum = pgEnum('role', ['admin', 'student']);
export const users = pgTable('user', {
id: serial('id').primaryKey(),
emailValidated: boolean('email_validated').default(false),
email: text('email').unique('user_email_unique'),
firstName: text('first_name'),
lastName: text('last_name'),
password: text('password'),
role: roleEnum('role').default('student'),
createdAt: timestamp('created_at'),
updatedAt: timestamp('updated_at'),
});
export type User = typeof users.$inferSelect;
export type NewUser = typeof users.$inferInsert;
Is there an intermediate solution for this other than reducing drizzle-orm version ?
I'm having this issue and then some:
$inferInsertonly provides fields that arenotNull()or generated by default$inferSelectdoes not respect nullability of fields and the type has every field as required
Also on 0.32.0 orm & 0.23.0 kit
I'm trying to add examples for @xegulon and @davieoba to the type tests. I can confirm that I see all the types properly. There might be an issue with the TypeScript setup. Could you share your tsconfig files and TypeScript version so I can reproduce the issue properly?
Sure!
tsconfig below, typescript is version ^5.3.3
{
"compilerOptions": {
"outDir": "dist",
"incremental": true,
"target": "es2017",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"noUnusedLocals": false,
"noImplicitAny": false,
"sourceMap": true,
"lib": ["ESNext"],
"strictNullChecks": false,
"inlineSources": true,
// Set `sourceRoot` to "/" to strip the build path prefix
// from generated source code references.
// This improves issue grouping in Sentry.
"sourceRoot": "/"
},
"compileOnSave": true,
"include": ["src"]
}
Sure, here it is @AndriiSherman:
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "ES2021",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": false,
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false
}
}
And "typescript": "5.5.3".
oh you didn't ask for mine lol sorry. but i have exactly the same issue and there's my setup anyway
Is there an intermediate solution for this other than reducing drizzle-orm version ?
Defining your insert type manually can be a solution maybe?
Agreed. Defining the insert types manually and declaring any type while inserting/updating the records.
db.insert(users).values(userObj as any)
db.update(users).set(userObj as any)
I'm having this issue and then some:
* `$inferInsert` only provides fields that are `notNull()` or generated by default * `$inferSelect` does not respect nullability of fields and the type has every field as requiredAlso on 0.32.0 orm & 0.23.0 kit
Hi! I have the same issue described here, and I have a feeling that this is a problem coming from TS, because it's not only affecting drizzle but also affecting others libraries inferring types like elysiajs. The main thing to note is that the issue appeared after I migrated to a NX monorepo. Now, my TS config is split between multiple files that are extending and referencing each others. I tried downgrading TS or drizzle, but the issue is still here. I will try to make a reproduction repo.
EDIT: Even my projects that aren't in a monorepo are affected. For now, I downgraded to 0.28.6 until a solution is found.
Once I put strict: true in TS config it infers properly... this might be another type of issue that you get when dealing with null types on a codebase that doesn't care about nulls... I'm lucky the codebase is small and I can enable strict stuff, others might not be 😞
It looks like a problem with excluding generated columns without strictNullChecks.
https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-orm/src/operations.ts#L11
@Grohden even with strict: true, I have the same problem.
@xegulon did you try strict + strictNullChecks?
is there any solution for this issue?
@Grohden yes I tried, same problem
@xegulon did you try strict + strictNullChecks?
I am having the same issue as well
Same here. Nullable columns aren't shown with $inferInsert. When changing strict to true in tsconfig.json it works, but shows weird errors in other places.
@AndriiSherman @Angelelz The issue affecting $inferSelect looks to be the evaluation of the OptionalKeyOnly type.
It looks to me like the discriminator evaluating the GeneratedType type (recent addition) is expecting to find
T['_']['generated']['type'] as the constant type byDefault a lot more than it is.
For me, I'm consistently seeing the T['_']['generated']['type'] as 'always' | 'byDefault'.
This is the code: https://github.com/drizzle-team/drizzle-orm/blob/90c4788980ab56c52da8504ef4fad9159c4113b0/drizzle-orm/src/operations.ts#L16-L22
I've modified part of the test from
T['_']['generated']['type'] extends 'byDefault' ? TKey : never
to
T['_']['generated']['type'] extends 'always' ? never: TKey
This seems to be a workable workaround (for me) but I've not attempted to examine the broader "generated type" code. Your expertise would be appreciated.
@Grohden I retried with strictNullChecks true, and I do not have the problem anymore, please excuse the first message that contradicts this.
@AndriiSherman I do not know if keeping this issue is relevant anymore, because I personally do not have the problem but some still seem to have it
Adding strictNullChecks true works for me too
@AndriiSherman Is strictNullChecks a requirement for Drizzle?
strictNullChecks fixes the type issue but breaks a lot of other code for me. I would be extremely happy about a fix for this. @danielsharvey got a very good finding for this.
Quite sure this is related: https://github.com/drizzle-team/drizzle-orm/issues/2694
I'm having this issue and then some:
$inferInsertonly provides fields that arenotNull()or generated by default$inferSelectdoes not respect nullability of fields and the type has every field as requiredAlso on 0.32.0 orm & 0.23.0 kit
Only God knows why $inferSelect isn't the type used in the update function
I'm having issues updating a table that has a default value enum and running into type errors even after trying both with notNull() and without.
just launching the idea for who might be thinking on fixing drizzle to not care about nulls depending on the flag:
type IsStrictNullEnabled = null extends number ? false : true
const a: IsStrictNullEnabled = true // will complain depending on the flag
ts will basically treat null as any when strict null is off, so you can just ask if null extends anything non any and if it it does, the flag is off
This also references bug #2694, which is a result of this bug I believe. This bug exists on version 0.36.1.