Skip to content

Commit bd09381

Browse files
committed
fix(builder): enhance createForeignKey to handle deprecated reference option and ensure proper assignment of reference properties
1 parent 32c3cfd commit bd09381

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎src/builder.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,16 @@ class ManageSQLBuilder extends Builder {
583583
}
584584

585585
createForeignKey(options, onCreateTable = false) {
586+
if (options.reference && !options.references) {
587+
options.references = options.reference;
588+
}
586589
if (options.references) {
590+
if (options.references.tableName && !options.references.table) {
591+
options.references.table = options.references.tableName;
592+
}
593+
if (options.references.columnName && !options.references.column) {
594+
options.references.column = options.references.columnName;
595+
}
587596
options.references.onDelete = options.references.onDelete ? options.references.onDelete.toUpperCase() : 'NO ACTION';
588597
options.references.onUpdate = options.references.onUpdate ? options.references.onUpdate.toUpperCase() : 'NO ACTION';
589598
}

‎tests/builder.tests.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,28 @@ describe('builder test case', () => {
916916
expect(sql).to.be.equal('ALTER TABLE `test_table` DROP FOREIGN KEY `fk_test`');
917917
});
918918

919+
it('should handle createForeignKey with deprecated reference (singular) option', () => {
920+
const builder = new ManageSQLBuilder({
921+
operator: 'create',
922+
target: 'foreignKey',
923+
name: 'fk_orders_user_id',
924+
table: 'orders',
925+
column: 'user_id',
926+
reference: {
927+
tableName: 'users',
928+
columnName: 'id',
929+
onDelete: 'CASCADE',
930+
onUpdate: 'RESTRICT'
931+
}
932+
});
933+
const sql = builder.sql;
934+
expect(sql).to.include('ALTER TABLE `orders`');
935+
expect(sql).to.include('FOREIGN KEY (`user_id`)');
936+
expect(sql).to.include('REFERENCES `users` (`id`)');
937+
expect(sql).to.include('ON DELETE CASCADE');
938+
expect(sql).to.include('ON UPDATE RESTRICT');
939+
});
940+
919941
it('should handle createColumns with uniqIndex', () => {
920942
const options = {
921943
operator: 'create',

0 commit comments

Comments
 (0)