Nullable field cannot be inserted or updated

Hi! I've already broken my head, I've asked several ai chats why this might be an error, but they don't know why the error occurs The problem is that I can't create/update the channels record's userId fields when the userId isn't set to .notNull() When I bet .notNull() everything works for me, but I need the user Id to be an optional field
export const channels = pgTable('channels', {
id: integer().primaryKey().generatedAlwaysAsIdentity(),
channelId: text('channelId').notNull().unique(),

userId: integer('userId'),

isActive: boolean('isActive').default(false).notNull(),

createdAt: timestamp('created_at').notNull().defaultNow(),
updatedAt: timestamp('updated_at').notNull().defaultNow(),
});

export const channelsRelations = relations(channels, ({ one }) => ({
user: one(users, {
fields: [channels.userId],
references: [users.id],
}),
}));
export const channels = pgTable('channels', {
id: integer().primaryKey().generatedAlwaysAsIdentity(),
channelId: text('channelId').notNull().unique(),

userId: integer('userId'),

isActive: boolean('isActive').default(false).notNull(),

createdAt: timestamp('created_at').notNull().defaultNow(),
updatedAt: timestamp('updated_at').notNull().defaultNow(),
});

export const channelsRelations = relations(channels, ({ one }) => ({
user: one(users, {
fields: [channels.userId],
references: [users.id],
}),
}));
Thank you in advance for any hints :silly:
No description
No description
5 Replies
Krimax
KrimaxOP3w ago
user.schema.ts
export const users = pgTable('users', {
id: integer().primaryKey().generatedAlwaysAsIdentity(),
discordId: text('discordId').notNull().unique(),
});

export const usersRelations = relations(users, ({ one, many }) => ({
channels: many(channels),
userChannelSettings: one(userChannelSettings),
}));
export const users = pgTable('users', {
id: integer().primaryKey().generatedAlwaysAsIdentity(),
discordId: text('discordId').notNull().unique(),
});

export const usersRelations = relations(users, ({ one, many }) => ({
channels: many(channels),
userChannelSettings: one(userChannelSettings),
}));
Krimax
KrimaxOP3w ago
I found a closed issue that seems to relate directly to my problem https://github.com/drizzle-team/drizzle-orm/issues/2654
GitHub
[BUG]: TypeScript error on optional fields during update (and inser...
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've encountered a TypeScript error when trying to update an optional field ...
Krimax
KrimaxOP3w ago
my tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"target": "ES2021",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": false,
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false
}
}
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"target": "ES2021",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": false,
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false
}
}
who solved this problem?
TOSL
TOSL3w ago
You added strict: true? Did you try also restarting you're TS language server?
Krimax
KrimaxOP3w ago
Yes the above is my config. I did everything that was offered in that issue on github If you don't have such a problem, please upload your package versions

Did you find this page helpful?