【发布时间】:2017-06-27 22:38:12
【问题描述】:
刷新我的迁移后,我在用户迁移和帐户类型用户表之间遇到问题,无法找出问题。
错误
[Illuminate\Database\QueryException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'account_typ
es' already exists (SQL: create table `account_types` (`id` int unsigned no
t null auto_increment primary key, `name` varchar(50) not null, `created_at
` timestamp null, `updated_at` timestamp null) default character set utf8mb
4 collate utf8mb4_unicode_ci)
[PDOException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'account_typ
es' already exists
我的迁移
帐户类型
public function up()
{
Schema::create('account_types', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 50);
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('account_types');
}
用户
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('surname', 20);
$table->string('email')->unique();
$table->string('password');
$table->string('mobilephone', 9);
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('users');
}
用户和帐户类型之间的关系,我相信问题可能就在这里,但仍然无法弄清楚迁移代码有什么问题。
用户和帐户类型的关系
public function up()
{
Schema::table('users', function($table) {
$table->integer('account_type_id')->unsigned();
$table->foreign('account_type_id')
->references('id')->on('account_types');
});
}
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropForeign('users_account_type_id_foreign');
$table->dropColumn('account_type_id');
});
}
【问题讨论】:
-
您如何检查迁移运行的顺序。运行
php artisan migrate:rollback将从您migrations表的底部开始,然后继续运行
标签: mysql laravel laravel-5 laravel-5.3 database-migration