Drop unneeded column via codegen
Hi! I'm refactoring a resource and removed all references to
office_id
from my ParkingLot
resource, replacing it with a belongs_to :site
relationship. However, when I run mix ash.codegen
, the migration generator is renaming the column instead of dropping the old one and creating a new one:
I responded yes during this question:
Is there a way to get the migration generator to properly drop the old column and create a fresh site_id
column? Or do I need to manually write this migration?
My current resource only has the belongs_to :site
relationship with no traces of office_id
anywhere.Solution:Jump to solution
When I clicked no, the generator added the new column but left the old one, which is fine to be honest. What I ended up doing is first removing all references of the old relationship, then running
codegen
. This removed the old column. Then I added the new relationships to the new table and ran codegen
again. Worked perfect, and I kinda like that it forced me to make more granular migrations to drop and then create.8 Replies
If you rename a column it will do a rename command so no need to drop the old column.
@Zach Daniel thank you and that is true. I wonder however if the column will maintain old data that no longer points to IDs in the correct table
Yeah, it would likely
Well, it wouldn't work in real life
the mgiration would fail
due to foreign keys etc.
If you were to answer no to that prompt then it would drop the old column and create the new one instead of using
rename
Solution
When I clicked no, the generator added the new column but left the old one, which is fine to be honest. What I ended up doing is first removing all references of the old relationship, then running
codegen
. This removed the old column. Then I added the new relationships to the new table and ran codegen
again. Worked perfect, and I kinda like that it forced me to make more granular migrations to drop and then create.It left the old one?
Wait are you using ash_sqlite?
yes it did, I am using ash_postgres
Hmm...that's pretty strange
Is it there in the migration but commented out perhaps?
I no longer have it 🙁
Once I found the solution I deleted the unused migration