ykisana
ykisana
DTDrizzle Team
Created by ykisana on 10/19/2024 in #help
Conditional NotNull Column?
I have a 'users' table. User can have both phone number or email, or either, but both fields can't be null. I tried adding the following constraint to my table: sql "CHECK (email IS NOT NULL OR phone_number IS NOT NULL)" But running into this issue on insert: https://github.com/drizzle-team/drizzle-orm/issues/2694 Is there a better way to do this?
1 replies
DTDrizzle Team
Created by ykisana on 8/16/2024 in #help
table with multiple possible one-to-many relation, but not many-to-many
I have an image table which looks like this, it has many possible one-to-many relationships, ie. room, profile and feedback can have many images.
export const image = pgTable('image', {
id: guid('id').defaultRandom().notNull(),
imageUrl: varchar('image_url', { length: 255 }).notNull(),

roomId: uuid('room_id').references(() => room.id, {
onDelete: 'cascade',
}),
profileId: uuid('profile_id').references(() => profile.id, {
onDelete: 'cascade',
}),
feedbackId: uuid('feedback_id').references(() => feedback.id, {
onDelete: 'cascade',
}),
});
export const image = pgTable('image', {
id: guid('id').defaultRandom().notNull(),
imageUrl: varchar('image_url', { length: 255 }).notNull(),

roomId: uuid('room_id').references(() => room.id, {
onDelete: 'cascade',
}),
profileId: uuid('profile_id').references(() => profile.id, {
onDelete: 'cascade',
}),
feedbackId: uuid('feedback_id').references(() => feedback.id, {
onDelete: 'cascade',
}),
});
The issue is, I want the image to have only one relation at a time. So I don't want it to be possible for image to have both roomId and feedbackId as not null. Want to know if this is possible. Also is there a better way to do images? How would you do tables for a relation like this?
1 replies