Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Larvel迁移(错误号:150“;外键约束格式不正确”;)_Php_Html_Laravel 5 - Fatal编程技术网

Php Larvel迁移(错误号:150“;外键约束格式不正确”;)

Php Larvel迁移(错误号:150“;外键约束格式不正确”;),php,html,laravel-5,Php,Html,Laravel 5,当用户单击链接时,我试图使用列chatter\u discussion从数据库中获取特定数据,但我遇到以下错误: SQLSTATE[HY000]:一般错误:1005无法创建表 论坛。聊天室讨论(错误号:150“外键约束无效 格式不正确”)(SQL:alter tablechatter\u discussionadd 约束chatter\u discussion\u user\u id\u foreign外键 (user\u id)在更新时删除级联时引用users(id) 级联) 问题通常发生在迁

当用户单击链接时,我试图使用列
chatter\u discussion
从数据库中获取特定数据,但我遇到以下错误:

SQLSTATE[HY000]:一般错误:1005无法创建表
论坛。聊天室讨论
(错误号:150“外键约束无效 格式不正确”)(SQL:alter table
chatter\u discussion
add 约束
chatter\u discussion\u user\u id\u foreign
外键 (
user\u id
)在更新时删除级联时引用
users
id
) 级联)


问题通常发生在迁移到Laravel的新版本时,该版本在模式中定义主键时使用
bigIncrements
而不是
increments
。 在定义外键关系之前,还需要定义外键的类型

解决方法是定义外键的类型,然后定义外键关系,例如:

Schema::table('chatter_discussion', function (Blueprint $table) {
// first define the type of the foreign keys in the schema
$table->bigInteger('chatter_category_id')->unsigned(); // the id of the chatter category
$table->bigInteger('user_id')->unsigned(); // the id of the user
/*
or use:
$table->integer('chatter_category_id');
$table->integer('user_id');
if using older versions of laravel, whatever works
*/        
// THEN define foreign key relations
$table->foreign('chatter_category_id')->references('id')->on('chatter_categories')
                    ->onDelete('cascade')
                    ->onUpdate('cascade');
        $table->foreign('user_id')->references('id')->on('users')
                    ->onDelete('cascade')
                    ->onUpdate('cascade');
    });
对引用外键的其他表执行类似操作

注意在您的示例中,您没有在
chatter\u讨论
表中定义
id
列,然后在
chatter\u post
中引用该列。不确定您是否遗漏了它,或者它是在以前的迁移中定义的

public function up() {     Schema::create('companies', function (Blueprint $table) {         $table->bigIncrements('id');         $table->string('name');         $table->text('address');         $table->string('tel1');         $table->string('tel2');         $table->integer('owner');         $table->unsignedBigInteger('access_id');         $table->string('depot_number')->default(2);         $table->timestamps();           $table->foreign('access_id')->references('id')->on('accesses')             ->onDelete('cascade');     }); } 

public function up() {     Schema::create('accesses', function (Blueprint $table) {         $table->bigIncrements('id');         $table->string('type');         $table->string('description');         $table->timestamps();     }); } 
数据库/migrations文件夹中,按名称排序。然后确保create\u accesss\u table位于create\u companys\u table

您可以通过发布用户表的定义来帮助他人。谢谢,但我已经在迁移的两个表中定义了chatter\u category\u id和user\u idsuccessfully@AbdessatarGhoudi,如果已经定义了外键列,则列的数据类型可能不匹配(例如使用大增量而不是激励,反之亦然)。通常情况下,这就是问题所在。您可以编辑您的问题并添加这些迁移,这样我就可以知道出了什么问题并相应地回答
public function up() {     Schema::create('companies', function (Blueprint $table) {         $table->bigIncrements('id');         $table->string('name');         $table->text('address');         $table->string('tel1');         $table->string('tel2');         $table->integer('owner');         $table->unsignedBigInteger('access_id');         $table->string('depot_number')->default(2);         $table->timestamps();           $table->foreign('access_id')->references('id')->on('accesses')             ->onDelete('cascade');     }); } 

public function up() {     Schema::create('accesses', function (Blueprint $table) {         $table->bigIncrements('id');         $table->string('type');         $table->string('description');         $table->timestamps();     }); }