我正在使用Laravel 4.2架构生成器来创建一些引用对方的表,并且遇到一些问题。Laravel架构生成器主键作为外键
我有一个简化的ERD。请注意,只有相关列显示:
请注意,我不能以任何方式修改的tblcurrencies
和tbldomains
表,因为我开发一个模块挂接到现有系统中。
我努力实现以下目标:
- 的
extensions
表中包含有关tbldomains
表中的行额外的信息 - 的
prices
表包含某货币域的定价信息,与附加类型值(注册,更新,传输) - 我想使用外键,以便可以级联删除。
目前,我用下面的代码来创建两个表:
Capsule::schema()->create('extensions', function ($table) {
$table->engine = 'InnoDB';
$table->integer('relid', 10);
// ...
$table->primary(['relid']);
$table->foreign('relid')->references('id')->on('tbldomains')->onDelete('cascade');
});
Capsule::schema()->create('prices', function ($table) {
$table->engine = 'InnoDB';
$table->integer('relid', 10);
$table->integer('currency', 10);
$table->enum('type', ['domainregister', 'domainrenew', 'domaintransfer']);
// ...
$table->primary(['relid', 'currency', 'type']);
$table->foreign('relid')->references('relid')->on('extensions')->onDelete('cascade');
$table->foreign('currency')->references('id')->on('tblcurrencies')->onDelete('cascade');
});
为prices
表结果在下面的SQL查询的创建脚本:
create table `prices` (`relid` int unsigned null auto_increment primary key, `currency` int unsigned null auto_increment primary key, `type` enum('domainregister', 'domainrenew', 'domaintransfer') not null, ...) engine = InnoDB
而这又导致以下错误:
SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key
我也尝试将主键设置为唯一的,因为可能Laravel自动将主整数键设置为自动增量。
此外,我尝试设置列作为unsigned
和index
,通过this和this答案建议
如何停止的模式构建器设置relid
和currency
字段自动递增,因为它们仅仅是外键?