Rails migration error

This seems pretty straight forward, but I'm not sure what is going wrong.

I am trying to do the following in my Rails migration:

change_column :foo, :bar, :text, :limit => 16777215 

I get 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 understand is the problem is that this change_column happens shortly after I added the column to foo and had to change it from type: string to enter text: in the first place. Each of them comes from its own migration scenarios and looks like this:

 add_column :foo, :bar, :string, :null => false 

and

 change_column :foo, :bar, :text 

As an experiment, I tried to change the first change_column (change_column: foo ,: bar ,: text) and found that this successfully modifies the table. Unfortunately, I cannot change any of the previous migrations and add only new ones to our current implementation so that they do not work in production. The question is, what allows me to change a column once, but not twice?

Update . I tried the first sentence, 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 '' 
+6
source share
6 answers

try

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

+12
source

If someone stumbles upon this post and finds it useful. I had the same problem as another way to avoid mysql configuration changes, so sql mode is not strict, i.e. Does not include STRICT_TRANS_TABLES , which it does by default.

+3
source

Do you have other migrations where :limit given :text , which works?

It is possible that :text does not accept :limit , and it just maps to a particular MySQL data type if I read it correctly.

Rails map redirection types and MySQL data types: http://www.orthogonalthought.com/blog/index.php/2007/06/mysql-and-ruby-on-rails-datatypes/

Types of MySQL TEXT (without mentioning LIMIT, but this does not exclude, I suppose: http://dev.mysql.com/doc/refman/5.0/en/string-type-overview.html

+1
source

For me, this is similar to the result of the transition from MySQL 5.5.x to 5.6.x

Note. Someone should read Semantic Versioning 2.0.0

My fix was pretty simple ...

Was

 change_column :my_table, :my_column, :mediumtext #=> Migrations Explosion 

IS

 change_column :my_table, :my_column, :mediumtext, default: nil #=> All good in the mysql 5.6.21 
+1
source

Try the following:

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

When used :null => false (as in the old migration), Rails adds the DEFAULT bit to the ALTER TABLE statement. But, as the error says, TEXT columns cannot have a DEFAULT value. Changing it to :null => true in the new migration, the problem should go away.

0
source

This worked for me:

  change_column :delayed_jobs, :handler, :text, limit: 16777215, null: true, default: nil 

I had to add default: nil , and then Rails was in the null: true order, which removes the default value defined during the earlier migration.

0
source

Source: https://habr.com/ru/post/887653/


All Articles