简体   繁体   English

Laravel Migration在数据进入表后添加字段?

[英]Laravel Migration add field after data is in table?

I have a migration called CreateItemsTable ; 我有一个名为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. 我不能只在迁移文件中添加一个字段并进行migrate:refresh因为我需要其中的数据。

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? 我不确定迁移是否比PhpMyAdmin更干净......或者我可能不理解它们?

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. 当然你可能会偶尔放下并添加列,但这没什么大不了的。

When you create a table for the first time you are probably using Schema::create() . 当您第一次创建表时,您可能正在使用Schema::create() All subsequent migrations for that table should use Scheme::table() . 该表的所有后续迁移都应使用Scheme::table() It accepts the same parameters except it doesn't attempt to create the table first. 它接受相同的参数,除了它不首先尝试创建表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM