Is there a valid use case for being able to specify a relation name that points to a "wrong" table?

Example:
export const firstTable = pgTable('first', {
id: text('id').primaryKey(),
});

export const secondTable = pgTable('second', {
id: uuid('id').primaryKey(),
});

export const referencingTable = pgTable('referencing', {
id: serial('id').primaryKey(),
firstId: text('first_id').references(() => firstTable.id),
secondId: uuid('second_id').references(() => secondTable.id),
});

const referencingRelations = relations(referencingTable, ({ one }) => ({
first: one(firstTable, { fields: [referencingTable.firstId], references: [firstTable.id], relationName: 'first_relation' }),
second: one(secondTable, { fields: [referencingTable.firstId], references: [secondTable.id], relationName: 'second_relation' })
}));

const firstRelations = relations(firstTable, ({ many }) => ({
referencing: many(referencingTable, { relationName: 'second_relation' }), // why can I do this??
}));
export const firstTable = pgTable('first', {
id: text('id').primaryKey(),
});

export const secondTable = pgTable('second', {
id: uuid('id').primaryKey(),
});

export const referencingTable = pgTable('referencing', {
id: serial('id').primaryKey(),
firstId: text('first_id').references(() => firstTable.id),
secondId: uuid('second_id').references(() => secondTable.id),
});

const referencingRelations = relations(referencingTable, ({ one }) => ({
first: one(firstTable, { fields: [referencingTable.firstId], references: [firstTable.id], relationName: 'first_relation' }),
second: one(secondTable, { fields: [referencingTable.firstId], references: [secondTable.id], relationName: 'second_relation' })
}));

const firstRelations = relations(firstTable, ({ many }) => ({
referencing: many(referencingTable, { relationName: 'second_relation' }), // why can I do this??
}));
As far as I can tell, unlike specifying a relation name which doesn't exist, or not specifying a relation name in case of ambiguity, which error in drizzle, this dies with a database driver error at runtime when the generated query references the wrong table in case of a type mismatch, or potentially continues to operate with undefined behavior resulting from selecting a row from the wrong table. With the above configuration, with the types deliberately incompatible, I receive PostgresError: operator does not exist: text = uuid when attempting to select using the wrong name.
1 Reply
francis
francisOP2w ago
(bump)

Did you find this page helpful?