【问题标题】:Use Laravel's migration to manage db schema from a single config file使用 Laravel 的迁移从单个配置文件管理数据库模式
【发布时间】:2016-09-06 20:18:43
【问题描述】:

我想使用 Laravel 的迁移来管理我的数据库。但我需要拥有一个用于存储架构的配置文件。像这样:

        {
           user: {
                    "id":"increments",
                    "name":"string",
                    "timestamps":"timestamps()"
                 }
        }

当我再次将此文件更改为下面的文本时

        {
           user: {
                    "id":"increments",
                    "name":"string",
                    "password":"string",
                    "timestamps":"timestamps()"
                 }
        }

我希望能够运行命令并更改数据库,而不会丢失任何数据或创建额外的配置文件。

我可以使用 laravel 迁移来实现这一点,或者如果您知道任何其他可以帮助我的解决方案,并且我可以在 laravel 上使用它而不会丢失任何 laravel 的数据库管理工具,请发表评论。 谢谢。

【问题讨论】:

  • 不幸的是,这不是迁移的工作方式。它们是完全线性的,以前的迁移永远不会知道未来的迁移。这也阻止了迁移系统跟踪更改(更改等),因此违背了迁移设计范式。
  • @Ohgodwhy 是的,我知道。这就是为什么我说我可能需要使用 Phinx 或其他更好的解决方案来完成我想做的事情。

标签: php laravel laravel-5 migration database-schema


【解决方案1】:

php artisan make:migration add_password_to_user(你确定这个表叫user而不是users?)

在您的新迁移中

public function up()
{
    Schema::table('user', function($table) {
        $table->string('password');
    });
}

不要忘记回滚

public function down()
{
    Schema::table('user', function($table) {
        $table->dropColumn('password');
    });
}

如果您有模型 User,该表应该是 users .. 否则请确保模型上有 protected $table = 'user';

https://laravel.com/docs/5.3/migrations

--table 和--create 选项也可用于指示表的名称以及迁移是否将创建新表。这些选项只是用指定的表预先填充生成的迁移存根文件:

php artisan make:migration create_users_table --create=users

php artisan make:migration add_votes_to_users_table --table=users

【讨论】:

  • 谢谢,但我不认为你理解我的问题 :)
  • 这不能回答问题。
猜你喜欢
  • 2013-08-10
  • 2015-10-11
  • 1970-01-01
  • 2016-12-09
  • 2015-09-12
  • 1970-01-01
  • 1970-01-01
  • 2014-06-05
  • 1970-01-01
相关资源
最近更新 更多