使用php artisan migrate创建单个表

问题描述 投票:0回答:4

我已执行此命令来创建“用户”表:

php artisan migrate

但之后我使用下面的命令创建了一个新的迁移文件,并希望创建一个“联系我们”表

php artisan make:migration contactus

在我执行下面的命令以生成“联系我们”表后,两个命令之后我都会收到以下错误:表'users'已经存在

php artisan migrate

这是我的迁移文件代码Contactus:

public function up()
    {
        Schema::create('contactus',function(Blueprint $table){
            $table->increments('id');
            $table->string('email');
            $table->string('message');
            $table->timestamp('created_at')->nullable();
        });
    }

有什么帮助我欣赏..

laravel
4个回答
1
投票

听起来像用户迁移文件已被编辑,需要重新迁移。运行:php artisan migrate:refresh

警告这将删除存储在数据库中的任何内容,因此请确保使用Seeder设置将任何相关内容放回数据库中。

如果您在重新迁移打开的AppServiceProvider上获得Laravel 5.4: Specified key was too long error并添加:

Schema::defaultStringLength(191);进入boot功能

确保在AppServiceProvider文件的顶部添加use Illuminate\Support\Facades\Schema;


1
投票

对于此问题SQLSTATE [42000]:语法错误或访问冲突:1071指定的密钥太长;最大密钥长度为767字节(SQL:alter table users添加唯一users_email_unique(email))在App / Providers文件夹下打开AppServiceProvider并添加以下代码:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Schema;
class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(191);
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

0
投票

尝试在命令下运行以再次删除和迁移所有表:

php artisan migrate:fresh

注意:如果您的用户表已经迁移或存在,并且您希望在命令下面创建一个新表:

php artisan make:migration contactus

然后

php artisan migrate

-1
投票

使用CLI时,请确保不要盲目复制其他迁移文件。

首先运行php artisan make:migration create_contact_us_table

现在打开/database/migrations/create_contact_us_table.php中的迁移文件位置

在这里编辑您的迁移,在UP功能中你应该有类似的东西

public function up()
{
    Schema::create('contact_us', function(Blueprint $table){
        $table->increments('id');
        //Other table content
    });
}

在此之后,您可以再次运行php artisan migrate来运行迁移。如果您不断收到此错误,请检查数据库和迁移表。它可能没有保存它已经运行您以前的迁移。如果是这种情况,请从数据库中删除所有表并再次运行php artisan migrate。应该工作。

© www.soinside.com 2019 - 2024. All rights reserved.