Laravel 使用 migrate 添加新表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31227478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:52:58  来源:igfitidea点击:

Laravel Add new Table with migrate

phpmysqllaravelmigration

提问by user3419778

I have created a Laravel project and I added few table with Migrate. It was working fine. The tables already had data. Now I require another table and I tried to add it with this command:

我创建了一个 Laravel 项目,并添加了一些带有 Migrate 的表。它工作正常。表已经有数据。现在我需要另一个表,并尝试使用以下命令添加它:

php artisan make:migration create_books_table

It is working and add files at \database\migrations.... I then added schema at up() function. But when I run the command,

它正在工作并在 \database\migrations 中添加文件...。然后我在 up() 函数中添加了模式。但是当我运行命令时,

php artisan migrate

It is not working , giving error Base table or view already exists.

它不起作用,给出错误 Base table or view already exists。

Please help. I am very new to laravel. Thanks

请帮忙。我对 Laravel 很陌生。谢谢

Migration code..

迁移代码..

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

class CreateBooksTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('books', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('name');
            $table->text('description');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('books');
    }

}

回答by mjmiri

create new folder in "database/migrations/" and use the "path" property on migrate , Once the table is migrated, you can move the migration file back to the usual folder.

在“database/migrations/”中创建新文件夹并使用 migrate 上的“path”属性,一旦表被迁移,您可以将迁移文件移回通常的文件夹。

php artisan migrate --path=/database/migrations/new folder/ 

回答by cre8

Laravel has an own table called migration. It's used to save information about when a table was created so artisan can rollback to another point. If you delete a table manually the entry in the migration table is still there. If you remove all tables in your database and migrate again it should work.

Laravel 有一个自己的表叫做迁移。它用于保存有关何时创建表的信息,以便工匠可以回滚到另一个点。如果您手动删除表,迁移表中的条目仍然存在。如果您删除数据库中的所有表并再次迁移,它应该可以工作。

回答by ameer nagvenkar

You need to delete the existing 'books' table in the database;

您需要删除数据库中现有的“books”表;

The table already exists so it wont get created again.

该表已存在,因此不会再次创建。

Or if you donot have too much data in the other tables run

或者如果您在其他表中没有太多数据运行

php artisan migrate:fresh

This will roll back previous table migrations and re migrate them all

这将回滚以前的表迁移并重新迁移它们