我想编制一个库存系统,需要3个表格。这些也可以通过没有外键的工匠生成。但是,只要我想添加外键,我就会收到以下错误消息。
SQLSTATE[HY000]: General error: 1005 Can't create table `inventar`.`#sql-fd4_141` (errno: 150 "Foreign key constraint is incorrectly formed")
(SQL:alter table items
添加约束items_lend_foreign
外键(lend
)引用lending
(id
))
这是我的代码:
项目表
Schema::create('items', function (Blueprint $table) {
$table->string('barcode');
$table->primary('barcode');
$table->string('name');
$table->string('description')->nullable();
$table->string('room')->nullable();
$table->string('status')->nullable();
$table->string('annotation')->nullable();
$table->string('image')->nullable();
$table->integer('lend')->unsigned()->nullable();
$table->string('manufactor')->nullable();
$table->timestamps();
});
Schema::table('items',function ($table){
$table->foreign('lend')->references('id')->on('lending');
});
借贷表
Schema::create('lending', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->integer('personid')->unsigned();
$table->dateTime('startdate');
$table->dateTime('enddate');
$table->timestamps();
});
Schema::table('lending',function ($table){
$table->foreign('barcode')->references('barcode')->on('items');
$table->foreign('personid')->references('personid')->on('persons');
});
人-表
Schema::create('persons', function (Blueprint $table) {
$table->integer('personid')->unsignd()->primary();
$table->string('firstname');
$table->string('lastname');
$table->string('email');
$table->string('annotation')->nullable();
$table->timestamps();
});
我也用谷歌搜索,但没有找到适用于我的解决方案。
我的主键是字符串是否存在问题?
答案 0 :(得分:0)
你的问题出现在
行 Schema::create('persons', function (Blueprint $table) {
$table->integer('personid')->unsignd()->primary();
}
从 unsignd()将其更改为 unsigned(),它会正常工作。