【发布时间】:2022-07-19 17:20:42
【问题描述】:
正在尝试迁移用户表。 DB 已经有 1 张桌子。联系表格是Contacts。尝试迁移 users 表。使用终端 $ php artisan make:migration create_users_table
创建了我的表
里面有如下代码。
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('username')->unique();
$table->timestamp('email_verified_at')();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
};
当我尝试迁移表终端时返回错误。
我运行 php artisan migrate 并返回
Migrating: create_contacts_table
“我正在尝试迁移 users 表,因为联系人表已经存在,所以它说
table already exists : 1050 ..."
试图按照网站上所说的那样给出一个论点,但也没有奏效。
我试过这个
$ php artisan migrate [--path[C:\xampp\htdocs\custom\database\migrations\2022_05_03_121341_create_users_table.php]]
但它返回“迁移”命令没有预期的参数
如何更改 migrate 命令的目录?或者我该如何解决这个问题。
【问题讨论】:
-
迁移特定迁移使用以下命令完成:
php artisan migrate --path=/database/migrations/full_migration_file_name_migration.php至于1050错误,请发布完整的迁移文件。 -
您似乎还有一个
contracts迁移,该迁移以前没有通过迁移运行(无论出于何种原因),但您已经创建了迁移。如果这只是开发环境并且您不关心数据丢失,请尝试运行php artisan migrate:fresh。 不要在生产环境中运行,或者如果您想将数据保留在数据库中 -
@geertjanknapen 我试过了,但它返回了这个错误是什么原因造成的? ibb.co/Fzn13Bc
-
@apokryfos 是的,我想我之前遇到过这个错误,但我能够将表迁移到数据库。这是一个开发环境,但我在
contacts表中有大约 20-30 个数据。是清空所有表还是清空数据库? -
@geertjanknapen 我编辑了问题并在我的迁移文件中添加了所有内容。
标签: php laravel artisan-migrate