gpt4 book ai didi

php - 外键相关的 Laravel 5 迁移错误

转载 作者:行者123 更新时间:2023-11-30 23:56:44 26 4
gpt4 key购买 nike

这是通过使用包含以下内容的 SQL 转储移植旧项目来学习 Laravel(特别是 v5.x)的尝试:

CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`locationid` int(11) NOT NULL,
`username` varchar(45) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`email` varchar(200) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`firstname` varchar(100) NOT NULL,
`lastname` varchar(100) NOT NULL,
`password` varchar(255) NOT NULL,
`active` bit(1) DEFAULT b'1',
`token` varchar(100) DEFAULT NULL,
`token_created` timestamp NULL DEFAULT NULL,
`role` varchar(45) NOT NULL DEFAULT 'user',
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `email_UNIQUE` (`email`),
UNIQUE KEY `username_UNIQUE` (`username`),
KEY `location_idx` (`locationid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

在转储的底部有这个与这个特定的表相关:

ALTER TABLE `user`
ADD CONSTRAINT `location_userfk` FOREIGN KEY (`locationid`) REFERENCES `location` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;

来自几个 stackoverflow 问题 1 , 2 , 3 ;我修改了我的原始代码,将 Schema::table 的外键从 Schema::create 中分离出来,并将 unsigned() 添加到每个字段:

public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->integer('locationid')->unsigned();
// $table->foreign('locationid')->references('id')->on('location');
$table->string('username', 60)->unique();
$table->string('email', 200)->unique();
$table->string('firstname', 100);
$table->string('lastname', 100);
$table->string('password', 255);
$table->boolean('active')->default(TRUE);
$table->string('role', 45)->default('user');
// $table->string('token', 255)->nullable()->default(NULL);
// $table->string('token_create')->nullable()->default(NULL);
$table->rememberToken();
$table->timestamps();
});

Schema::table('users', function(Blueprint $table)
{
$table->foreign('locationid')->references('id')->on('location');
});
}

但是我在使用新创建的空数据库进行迁移时仍然出现错误:

exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint' in /home/vagrant/projects/communityfuturesbc/vendor/laravel/framework/src/Illuminate/Database/Connection.php:358
Stack trace:
#0 /home/vagrant/projects/communityfuturesbc/vendor/laravel/framework/src/Illuminate/Database/Connection.php(358): PDOStatement->execute(Array) etc...

Next exception 'Illuminate\Database\QueryException' with message 'SQLSTATE[HY000]: General err
or: 1215 Cannot add foreign key constraint (SQL: alter table `users` add constraint users_loca
tionid_foreign foreign key (`locationid`) references `location` (`id`))' in /home/vagrant/proj
ects/communityfuturesbc/vendor/laravel/framework/src/Illuminate/Database/Connection.php:614
Stack trace:
#0 /home/vagrant/projects/communityfuturesbc/vendor/laravel/framework/src/Illuminate/Database/Connection.php(570): Illuminate\Database\Connection->runQueryCallback('alter table `us...', Ar
ray, Object(Closure))

根据MySQL意思是:

Error: 1215 SQLSTATE: HY000 (ER_CANNOT_ADD_FOREIGN)
Message: Cannot add foreign key constraint

之后,我无法回滚或再次迁移,而不会出现关于数据库中已存在用户表的另一种类型的错误,因此每次我尝试上述代码的一个版本时,我每次都会删除我的数据库。

我确定在修复此问题后,它会出现在所有其他表中,这些表相似或具有多个外键,因此希望无论修复此问题对其余表都有效。

最佳答案

您必须更改迁移顺序

|-database/
|--migrations/
|---2015_02_02_141725_create_users_table.php
|---2015_03_01_234626_create_locations_table.php

应该是:

|-database/
|--migrations/
|---2015_01_01_234626_create_locations_table.php
|---2015_02_02_141725_create_users_table.php

最后:

php artisan migrate

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com