我的迁移文件有错误。但是我找不到任何错误解决方案。当我运行迁移命令时,我在以下系统上收到该错误。
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table 'zadmin_elder.PowerCuts' (errno: 150) (SQL: create table `PowerCuts` (`CutID`
int unsigned not null auto_increment primary key, `Title` varchar(45) null, `Message` varchar(250) null, `CreatedAt` datetime not nu
ll, `UpdatedAt` datetime null, `DeletedAt` datetime null, `PlannedDate` date null, `StartTime` time null, `EndTime` time null, `TownI
D` int unsigned not null, `Distrcit` varchar(45) null, `Geolocation` varchar(45) not null, `GeoRadius` int unsigned not null) default
character set utf8 collate utf8_unicode_ci)
[PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table 'zadmin_elder.PowerCuts' (errno: 150)
这是我的create_powercut_table.php的创建函数;
Schema::create('PowerCuts', function(Blueprint $table)
{
$table->increments('CutID', true)->unsigned();
$table->string('Title', 45)->nullable();
$table->string('Message', 250)->nullable();
$table->dateTime('CreatedAt');
$table->dateTime('UpdatedAt')->nullable();
$table->dateTime('DeletedAt')->nullable();
$table->date('PlannedDate')->nullable();
$table->time('StartTime')->nullable();
$table->time('EndTime')->nullable();
$table->integer('TownID')->unsigned()->index('fk_PowerCuts_Towns1_idx');
$table->string('Distrcit', 45)->nullable();
$table->string('Geolocation', 45);
$table->integer('GeoRadius')->unsigned();
});
这是我的create_Foreign_powercut.php的创建函数;
Schema::table('PowerCuts', function(Blueprint $table){
$table->foreign('TownID', 'PowerCuts_ibfk_1')->references('TownID')->on('Towns')->onUpdate('CASCADE')->onDelete('CASCADE');
});
谢谢,问候
最佳答案
根据MySQL InnoDB Error Codes :
If the error message refers to error 150, table creation failed because a foreign key constraint was not correctly formed.
检查PowerCuts.TownID
和Towns.TownID
的类型是否完全相同(无符号整数),因为外键字段的类型必须与引用列的类型。
关于php - Laravel 迁移 150 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28874443/