Mysql 刷新数据库表迁移时出错
刷新“我的迁移”后,我发现用户迁移和帐户类型用户表之间存在问题,无法找出问题所在 错误Mysql 刷新数据库表迁移时出错,mysql,laravel,laravel-5,laravel-5.3,database-migration,Mysql,Laravel,Laravel 5,Laravel 5.3,Database Migration,刷新“我的迁移”后,我发现用户迁移和帐户类型用户表之间存在问题,无法找出问题所在 错误 [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
[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');
});
}
用户
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');
});
}
用户和帐户类型之间的关系,我相信问题可能在这里,但仍然无法找出迁移代码的错误
用户与账户类型的关系
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');
});
}
就像我以前多次看到的那样,在迁移时发生了一个错误,因为迁移脚本没有机会注册最后一次迁移,所以出现了这个错误 如果您不关心已经存储的数据,请手动删除数据库中的所有表(甚至是迁移表),然后重新运行 当您的表中有要保留的数据时,您应该检查此迁移表并查看批处理列。将要保留的每个条目都放在1上,并将该行及其下的行设置为0(也许删除它们也可以)。运行php artisan migrate。使用phpmyadmin时,您可以通过查询更新这些行,例如:
updatemigrations SET batch=0,其中的迁移类似于“%create\u users\u table”代码>
希望这有帮助:)。我想你应该试试这个:
首先从数据库的迁移表中删除帐户类型,用户
并刷新您的迁移
希望这对你有用 如何签出迁移的运行顺序。运行php artisan migrate:rollback
将从您的migrations
表的底部开始,并以备份的方式运行