php - Laravel 迁移 150 错误

标签 php laravel

我的迁移文件有错误。但是我找不到任何错误解决方案。当我运行迁移命令时,我在以下系统上收到该错误。

[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.TownIDTowns.TownID的类型是否完全相同(无符号整数),因为外键字段的类型必须与引用列的类型。

关于php - Laravel 迁移 150 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28874443/

相关文章:

laravel - 如何在 laravel 身份验证中处理自定义列名、用户名和密码

php - 使用 gmail(windows) 从本地主机发送电子邮件

php - DayofYear + Curdate 不起作用

php - 外键约束失败,因为插入语句尚未提交 (MySQL)

php - MYSQL 查询未执行或未显示错误

Laravel 5.8 安装错误在database.php第58行: Undefined class constant 'MYSQL_ATTR_SSL_CA'

php - 用户 'homestead' @'localhost' 的访问被拒绝(使用密码 : YES)

php - 'C :\wmic' is not recognized as an internal or external command, 可运行的程序或批处理文件

php - Laravel 4 - Eloquent 查询范围顺序

php - 如何将动态数组的矩阵创建为一个数组?