opelezim
opelezim
Explore posts from servers
DTDrizzle Team
Created by opelezim on 9/26/2024 in #help
db.insert is not reading all properties from my schema
No description
4 replies
DTDrizzle Team
Created by opelezim on 9/26/2024 in #help
db.insert is not reading all properties from my schema
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],
}),
}),
);
`
4 replies
DTDrizzle Team
Created by opelezim on 9/26/2024 in #help
db.insert is not reading all properties from my schema
As you can see, it reports an error
4 replies