Laravel迁移表已经存在,但是我想添加新的而不是旧的 [英] Laravel Migration table already exists, but I want to add new not the older

查看:47
本文介绍了Laravel迁移表已经存在,但是我想添加新的而不是旧的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前创建了users表.现在,我创建了一个新迁移,以在架构内创建一个新的books表.当我尝试运行命令时

I previously created users table. Now I have created a new migration to create a new books table inside my schema. When I try to run the command

php artisan migrate

它显示:

[Illuminate\Database\QueryException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' alre
ady exists (SQL: create table `users` (`id` int unsigned not null auto_incr
ement primary key, `username` varchar(255) not null, `email` varchar(255) n
ot null, `password` varchar(255) not null, `created_at` timestamp default 0
 not null, `updated_at` timestamp default 0 not null) default character set
 utf8 collate utf8_unicode_ci)

这是我的新迁移表:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateBooksTable extends Migration {
    public function up()
    {
        Schema::create('books', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('name');
            $table->string('auther');
            $table->string('area');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('books');
    }
}

如何消除错误?

推荐答案

在v5.x中,您可能仍然会遇到问题.因此,尝试先使用

In v5.x, you might still face the problem. So, try to delete related table manually first using

php工匠修补匠

然后

Schema :: drop('books')

(然后退出并显示 q )

现在,您可以成功地 php artisan migration:rollback php artisan migration .

Now, you can successfully php artisan migrate:rollback and php artisan migrate.

如果这种情况反复发生,则应检查迁移中的 down()方法是否显示正确的表名.(如果您更改了表名,可能会很麻烦.)

If this happens repeatedly you should check that the down() method in your migration is showing the right table name. (Can be a gotcha if you've changed your table names.)

这篇关于Laravel迁移表已经存在,但是我想添加新的而不是旧的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆