mtp*_*ltz 0 php mysql database laravel laravel-5
这是尝试通过使用包含以下内容的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 ;
Run Code Online (Sandbox Code Playgroud)
在转储的底部,这与此特定表有关:
ALTER TABLE `user`
ADD CONSTRAINT `location_userfk` FOREIGN KEY (`locationid`) REFERENCES `location` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
Run Code Online (Sandbox Code Playgroud)
从一对夫妇计算器问题1,2,3 ; 我已经修改了原始代码,从Schema :: create中分离出Schema :: table的外键,并将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');
});
}
Run Code Online (Sandbox Code Playgroud)
但是当我使用空的新创建的数据库进行迁移时仍然会出现错误:
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))
Run Code Online (Sandbox Code Playgroud)
根据MySQL的含义:
Error: 1215 SQLSTATE: HY000 (ER_CANNOT_ADD_FOREIGN)
Message: Cannot add foreign key constraint
Run Code Online (Sandbox Code Playgroud)
之后我无法回滚或再次迁移而没有得到关于DB中已存在用户表的其他类型的错误,因此每次我尝试上述代码的版本时,我每次都丢弃了我的数据库.
我确定在修复它之后它将发生在所有其他表中,它们是相似的或具有多个外键,所以希望无论如何修复这对其余的都有效.
小智 5
您必须更改迁移顺序
|-database/
|--migrations/
|---2015_02_02_141725_create_users_table.php
|---2015_03_01_234626_create_locations_table.php
Run Code Online (Sandbox Code Playgroud)
应该:
|-database/
|--migrations/
|---2015_01_01_234626_create_locations_table.php
|---2015_02_02_141725_create_users_table.php
Run Code Online (Sandbox Code Playgroud)
最后:
php artisan migrate
Run Code Online (Sandbox Code Playgroud)