How to fix this code? I want to reference recordId
to ids colums in the tickets
and packages
tables.
如何修复此代码?我想在ticket和packages表中引用recordId到ids colums。
createTable(`payments`, function(t){
t.increments();
t.integer('userId').references('id').inTable(`users`).unsigned().onDelete('cascade');
t.integer('recordId')
.references('id').inTable(`tickets`).unsigned().onDelete('cascade');
t.integer('recordId')
.references('id').inTable(`packages`).unsigned().onDelete('cascade');
t.string('peyFro');
t.string('gateway').notNullable();
t.string('authority').notNullable();
t.integer('amount').notNullable();
t.string('status').notNullable();
t.dateTime('createdAt').defaultTo(knex.fn.now());
})
1 个解决方案
#1
2
Try the foreign function:
尝试外来函数:
t.integer('recordId').unsigned();
t.foreign('recordId','payment_ticket_fk').references('tickets.id').onDelete('cascade');
t.foreign('recordId','payment_package_fk').references('packages.id').onDelete('cascade');
Linking one FK column to multiple PKs seems like bad design (that might lead to bad data) though. Perhaps you could just have two different FK columns.
将一个FK列链接到多个PK似乎是糟糕的设计(可能导致错误的数据)。也许你可以只有两个不同的FK列。
#1
2
Try the foreign function:
尝试外来函数:
t.integer('recordId').unsigned();
t.foreign('recordId','payment_ticket_fk').references('tickets.id').onDelete('cascade');
t.foreign('recordId','payment_package_fk').references('packages.id').onDelete('cascade');
Linking one FK column to multiple PKs seems like bad design (that might lead to bad data) though. Perhaps you could just have two different FK columns.
将一个FK列链接到多个PK似乎是糟糕的设计(可能导致错误的数据)。也许你可以只有两个不同的FK列。