db.insert is not reading all properties from my schema

I'm trying to insert a value into a table with several relationships, but Intelissense reports an error and the value is not inserted
No description
No description
2 Replies
opelezim
opelezimOP3mo ago
As you can see, it reports an error
import {
serial,
text,
timestamp,
pgTable,
varchar,
pgEnum,
integer,
} from 'drizzle-orm/pg-core';

import { relations, sql } from 'drizzle-orm';

export const messages = pgTable('messages', {
id: serial('id').primaryKey(),
message: varchar('message', { length: 256 }),
authorId: integer('author_id')
.notNull()
.references(() => users.id),
chatId: integer('chat_id')
.notNull()
.references(() => chats.id),
createdAt: timestamp('created_at')
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: timestamp('updated_at', { mode: 'string' })
.default(sql`CURRENT_TIMESTAMP`)
.$onUpdate(() => sql`CURRENT_TIMESTAMP`)
.notNull(),
});

export const chatParticipants = pgTable('chat_participants', {
id: serial('id').primaryKey(),
chatId: integer('chat_id')
.notNull()
.references(() => chats.id),
participantId: integer('participant_id')
.notNull()
.references(() => users.id),
});

export const userRoles = pgEnum('role', ['user', 'admin']);

export const users = pgTable('users', {
id: serial('id').primaryKey(),
name: varchar('name', { length: 256 }),
email: text('email').unique(),
password: text('password'),
createdAt: timestamp('created_at')
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: timestamp('updated_at', { mode: 'string' })
.default(sql`CURRENT_TIMESTAMP`)
.$onUpdate(() => sql`CURRENT_TIMESTAMP`)
.notNull(),
role: userRoles('role').default('user').notNull(),
});

export const userRelations = relations(users, ({ many }) => ({
messages: many(messages),
}));

export const chats = pgTable('chats', {
id: serial('id').primaryKey(),
name: varchar('name', { length: 256 }),
createdAt: timestamp('created_at')
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: timestamp('updated_at', { mode: 'string' })
.default(sql`CURRENT_TIMESTAMP`)
.$onUpdate(() => sql`CURRENT_TIMESTAMP`)
.notNull(),
});

export const chatRelations = relations(chats, ({ many }) => ({
messages: many(messages),
participants: many(chatParticipants),
}));

export const messagesRelations = relations(messages, ({ one }) => ({
author: one(users, {
fields: [messages.authorId],
references: [users.id],
}),
chat: one(chats, {
fields: [messages.chatId],
references: [chats.id],
}),
}));

export const chatParticipantsRelations = relations(
chatParticipants,
({ one }) => ({
chat: one(chats, {
fields: [chatParticipants.chatId],
references: [chats.id],
}),
participant: one(users, {
fields: [chatParticipants.participantId],
references: [users.id],
}),
}),
);
import {
serial,
text,
timestamp,
pgTable,
varchar,
pgEnum,
integer,
} from 'drizzle-orm/pg-core';

import { relations, sql } from 'drizzle-orm';

export const messages = pgTable('messages', {
id: serial('id').primaryKey(),
message: varchar('message', { length: 256 }),
authorId: integer('author_id')
.notNull()
.references(() => users.id),
chatId: integer('chat_id')
.notNull()
.references(() => chats.id),
createdAt: timestamp('created_at')
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: timestamp('updated_at', { mode: 'string' })
.default(sql`CURRENT_TIMESTAMP`)
.$onUpdate(() => sql`CURRENT_TIMESTAMP`)
.notNull(),
});

export const chatParticipants = pgTable('chat_participants', {
id: serial('id').primaryKey(),
chatId: integer('chat_id')
.notNull()
.references(() => chats.id),
participantId: integer('participant_id')
.notNull()
.references(() => users.id),
});

export const userRoles = pgEnum('role', ['user', 'admin']);

export const users = pgTable('users', {
id: serial('id').primaryKey(),
name: varchar('name', { length: 256 }),
email: text('email').unique(),
password: text('password'),
createdAt: timestamp('created_at')
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: timestamp('updated_at', { mode: 'string' })
.default(sql`CURRENT_TIMESTAMP`)
.$onUpdate(() => sql`CURRENT_TIMESTAMP`)
.notNull(),
role: userRoles('role').default('user').notNull(),
});

export const userRelations = relations(users, ({ many }) => ({
messages: many(messages),
}));

export const chats = pgTable('chats', {
id: serial('id').primaryKey(),
name: varchar('name', { length: 256 }),
createdAt: timestamp('created_at')
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: timestamp('updated_at', { mode: 'string' })
.default(sql`CURRENT_TIMESTAMP`)
.$onUpdate(() => sql`CURRENT_TIMESTAMP`)
.notNull(),
});

export const chatRelations = relations(chats, ({ many }) => ({
messages: many(messages),
participants: many(chatParticipants),
}));

export const messagesRelations = relations(messages, ({ one }) => ({
author: one(users, {
fields: [messages.authorId],
references: [users.id],
}),
chat: one(chats, {
fields: [messages.chatId],
references: [chats.id],
}),
}));

export const chatParticipantsRelations = relations(
chatParticipants,
({ one }) => ({
chat: one(chats, {
fields: [chatParticipants.chatId],
references: [chats.id],
}),
participant: one(users, {
fields: [chatParticipants.participantId],
references: [users.id],
}),
}),
);
`
opelezim
opelezimOP3mo ago
when i remove the field, all work:
No description
Want results from more Discord servers?
Add your server