This seems pretty straight forward but I'm not sure what is going wrong.
I'm attempting to do the following in my Rails migration:
change_column :foo, :bar, :text, :limit => 16777215
I'm getting the following error
Mysql::Error: BLOB/TEXT column 'bar' can't have a default value: ALTER TABLE `foo` CHANGE `bar` `email_contents` text(16777215) DEFAULT '' NOT NULL
The only thing I can figure is causing an issue is that this change_column is occurring shortly after I added the column to foo and had to change it from type :string to type :text in the first place. These each come from their own migration scripts and look like this:
add_column :foo, :bar, :string, :null => false
and
change_column :foo, :bar, :text
As an experiment, I tried changing the first change_column (change_column :foo, :bar, :text) and discovered that this successfully alters the table. Unfortunately I cannot change either of the previous migrations and can only add new ones in our current implementation so that will not work in production. The question is, what is allowing me to change the column once but not twice?
Update Tried the first suggestion but got the following:
Mysql::Error: BLOB/TEXT column 'bar' can't have a default value: ALTER TABLE `foo` CHANGE `bar` `bar` text(16777215) DEFAULT ''
The very first migration related rails command you will use will probably be bin/rails db:migrate. In its most basic form it just runs the change or up method for all the migrations that have not yet been run. If there are no such migrations, it exits.
This file is what Rails uses to deploy your application to a new database. So using migrations makes it possible for you to deploy your app to new platforms. You can develop on one database and deploy to another, or deploy to a new database platform in production.
Rails migrations free you from worrying about the differences between various SQL grammar so you can focus on your application code. So, take a closer look at migrations before you write SQL for your Rails app. Stackify’s APM tools are used by thousands of .NET, Java, PHP, Node.js, Python, & Ruby developers all over the world.
If you’re working with Active Records, Rails will create the migration for you. You can use all of the Rails basic data types with migrations, and it’ll be matched to the corresponding type in the database you migrate to. Here’s a list of data types:
try
change_column :foo, :bar, :text, :limit => 16777215, :default => nil, :null => true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With