Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions drizzle-orm/src/mysql-core/columns/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface ReferenceConfig {
onUpdate?: UpdateDeleteAction;
onDelete?: UpdateDeleteAction;
};
name?: string;
}

export interface MySqlColumnBuilderBase<
Expand All @@ -51,8 +52,8 @@ export abstract class MySqlColumnBuilder<

private foreignKeyConfigs: ReferenceConfig[] = [];

references(ref: ReferenceConfig['ref'], actions: ReferenceConfig['actions'] = {}): this {
this.foreignKeyConfigs.push({ ref, actions });
references(ref: ReferenceConfig['ref'], actions: ReferenceConfig['actions'] = {}, name: ReferenceConfig['name'] = undefined): this {
this.foreignKeyConfigs.push({ ref, actions, name });
return this;
}

Expand All @@ -75,11 +76,11 @@ export abstract class MySqlColumnBuilder<

/** @internal */
buildForeignKeys(column: MySqlColumn, table: MySqlTable): ForeignKey[] {
return this.foreignKeyConfigs.map(({ ref, actions }) => {
return ((ref, actions) => {
return this.foreignKeyConfigs.map(({ ref, actions, name }) => {
return ((ref, actions, name) => {
const builder = new ForeignKeyBuilder(() => {
const foreignColumn = ref();
return { columns: [column], foreignColumns: [foreignColumn] };
return { columns: [column], foreignColumns: [foreignColumn], name };
});
if (actions.onUpdate) {
builder.onUpdate(actions.onUpdate);
Expand All @@ -88,7 +89,7 @@ export abstract class MySqlColumnBuilder<
builder.onDelete(actions.onDelete);
}
return builder.build(table);
})(ref, actions);
})(ref, actions, name);
});
}

Expand Down
20 changes: 20 additions & 0 deletions integration-tests/tests/mysql/mysql-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ const users2Table = mysqlTable('users2', {
cityId: int('city_id').references(() => citiesTable.id),
});

const users3Table = mysqlTable('users3', {
id: serial('id').primaryKey(),
name: text('name').notNull(),
cityId: int('city_id').references(() => citiesTable.id, {}, "users3_city_id_fk"),
});

const citiesTable = mysqlTable('cities', {
id: serial('id').primaryKey(),
name: text('name').notNull(),
Expand Down Expand Up @@ -4440,6 +4446,20 @@ export function tests(driver?: string) {
expectTypeOf(rawRes).toEqualTypeOf<ExpectedType>();
expect(rawRes).toStrictEqual(expectedRes);
});

test('column builder: generated foreign key', (ctx) => {
const tableConfig = getTableConfig(users2Table);

expect(tableConfig.foreignKeys).toHaveLength(1);
expect(tableConfig.foreignKeys[0]!.getName()).toBe('users2_city_id_cities_id_fk');
});

test('column builder: provided foreign key', (ctx) => {
const tableConfig = getTableConfig(users3Table);

expect(tableConfig.foreignKeys).toHaveLength(1);
expect(tableConfig.foreignKeys[0]!.getName()).toBe('users3_city_id_fk');
});
});

test('insert into ... select', async (ctx) => {
Expand Down