Laravel Migration在表中添加数据后添加字段? [英] Laravel Migration add field after data is in table?

查看:954
本文介绍了Laravel Migration在表中添加数据后添加字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为CreateItemsTable的迁移;我运行了那个表中的项目,现在我需要在表中添加一个新字段.我不能只向迁移文件和migrate:refresh添加字段,因为我需要其中的数据.

I have a migration called CreateItemsTable; I ran that, I have items in that table, now I need to add a new field to the table. I can't just add a field to the migration file and migrate:refresh because I need the data that's in it.

我是否应该再进行一次迁移以添加字段?当我在开发中测试事物时,这似乎是一团糟,我可能会改变很多领域.我不确定迁移是否比PhpMyAdmin更干净...或者我听不懂?

Am I supposed to make another migration for adding a field? That's seems like mess while I'm testing things in development, I might change fields a lot. I'm not sure if migrations are cleaner than just PhpMyAdmin... or maybe I don't understand them?

推荐答案

是的,每次您需要以某种方式更改表时,都需要为其创建新的迁移.这就是迁移的重点.当您在协作环境中进行开发并从远程存储库中进行一些更改时,您应该做的一件事情(如果使用数据库)是运行其他开发人员可能创建的所有迁移.这样可以使您的数据库保持同步.

Yes, each time you need to change a table in some way you'd create a new migration for it. That's the whole point of migrations. When you're developing in a collaborative environment and you pull down some changes from a remote repository, one of the things you should do (if working with a database) is run any migrations that other developers might have created. This keeps your databases in sync.

当然可以偶尔删除并添加列,但这没什么大不了的.

Sure you might drop and add columns occasionally but it's no big deal.

首次创建表时,可能使用的是Schema::create().该表的所有后续迁移都应使用Scheme::table().它接受相同的参数,只是不先尝试创建表.

When you create a table for the first time you are probably using Schema::create(). All subsequent migrations for that table should use Scheme::table(). It accepts the same parameters except it doesn't attempt to create the table first.

这篇关于Laravel Migration在表中添加数据后添加字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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