Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails migration error

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 ''
like image 788
keybored Avatar asked May 09 '11 19:05

keybored


People also ask

What does bin/rails DB migrate do?

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.

What are migrations in rails?

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.

Why should you migrate your rails app to SQL?

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.

How do I migrate data from one rails database to another?

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:


1 Answers

try

change_column :foo, :bar, :text, :limit => 16777215, :default => nil, :null => true

like image 64
mkly Avatar answered Oct 27 '22 17:10

mkly