Laravel 5.2 how to update migration without losing data

泄露秘密 提交于 2019-12-03 04:16:54

问题


I'm using laravel 5.2 and I usually update my database according to project requirements, so I'd like to do it without losing database records. I don't mean how to seed my database.. I mean when my database is live and I want to update it throw laravel migration. I was going throw Laravel Documentation but I found nothing, so I hope to find somebody help me


回答1:


Since you already have data in your tables then instead of rollback your migrations (which will cause data losses) you can create new migration files to update your tables. Suppose you have a table users with columns name, email, password. You stored data in that table. Then you realized that you also do need to add a new column named mobile_no to your users table. To do this you need to create a new migration file. Command will be:

php artisan make:migration add_mobile_no_columns_to_users_table --table=users

This way a new migration file will be created. Set your column details there, run the migrations using php artisan migrate and that's all. You'll have this new column in your users table without losing previously stored data.




回答2:


Please note that when you add a new column for a table while already there are data, you have to set a default value for new column or make it nullable type.. otherwise you will endup with error




回答3:


Make sure that when you are adding new column in your table, that column should be nullable, and should not be unique. Otherwise you will face error. Because when a new column is created it will be empty(not unique). In that condition you have to rollback the migration.



来源:https://stackoverflow.com/questions/39934276/laravel-5-2-how-to-update-migration-without-losing-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!