Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRobert Mylne
13,708 PointsUpdating database.
In the video it shows how to rollback a migration, make a change then re migrate.
How do you update a table without deleting all the data, ie. change 'name' column in the db to 'title' but still keep all the same info in the column?
2 Answers
Mike Costa
Courses Plus Student 26,362 PointsIf you are using Laravel Migrations, you can create a new migration and inside of that you would use the Schema Builder Laravel provides.
Inside the up function, you would do:
Schema::table('TABLE NAME GOES HERE', function($table)
{
$table->renameColumn('name', 'title');
});
And inside the down function, you would put the opposite of what you did in the up function:
Schema::table('TABLE NAME GOES HERE', function($table)
{
$table->renameColumn('title', 'name');
});
This way if you need to use the rollback functionality, this is a clean way to do it.
Gloria Dwomoh
13,116 PointsYou can do that with... "ALTER TABLE....". I am not sure if you are using MySQL or something else, but you can check the documentation of it and see how it can be written. For example if you are using MYSQL and your table name is "students" you could do that by using this command "alter table students change name title varchar (40) ;" If you are using some other means to create the database, check its documentation to see how you can alter a table.
Robert Mylne
13,708 PointsRobert Mylne
13,708 PointsThanks that was what I was looking for :)
Mike Costa
Courses Plus Student 26,362 PointsMike Costa
Courses Plus Student 26,362 PointsGlad I can help :)
Robert Mylne
13,708 PointsRobert Mylne
13,708 PointsHey, I have just got around to trying this and nothing happens. Do you know what I could be doing wrong? On the laravel site it says "Before renaming a column, be sure to add the doctrine/dbal dependency to your composer.json file." But isn't this automatically added with homestead?
Gloria Dwomoh
13,116 PointsGloria Dwomoh
13,116 PointsHi Robert, if you are using that framework according to its documentation you have to add it on your own. The documentation says....
"To rename a column, you may use the renameColumn method on the Schema builder. Before renaming a column, be sure to add the doctrine/dbal dependency to your composer.json file."
If you are unsure of how to do that. You can check this out, it seems pretty helpful.
Mike Costa
Courses Plus Student 26,362 PointsMike Costa
Courses Plus Student 26,362 PointsAfter you make the migration, you still have to migrate it by using php artisan migrate.