[Ruby] When adding a null constraint to a table

An error occurs when trying to migrate by writing the necessary information in the migration file. I think it's strange, but first try resetting.

% rails db:migrate:reset 

↓ ↓ ↓ Results below ↓ ↓ ↓
Dropped database 'protospace_32411_development'
Dropped database 'protospace_32411_test'
Created database 'protospace_32411_development'
Created database 'protospace_32411_test'
== 20201214092753 DeviseCreateUsers: migrating ================================
-- create_table(:users)
rails aborted!
StandardError: An error has occurred, all later migrations canceled:

Mysql2::Error: BLOB/TEXT column 'profile' can't have a default value
#The following is omitted

Something is wrong··· But I don't know what it is. Let's try again.

% rails db:migrate 

↓ ↓ ↓ Results below ↓ ↓ ↓

== 20201214092753 DeviseCreateUsers: migrating ================================
-- create_table(:users)
rails aborted!
StandardError: An error has occurred, all later migrations canceled:

Mysql2::Error: BLOB/TEXT column 'profile' can't have a default value
#The following is omitted

Is it no good? If you look closely at the output result of the terminal here, you will finally notice the same error statement.

Mysql2::Error: BLOB/TEXT〜〜

After investigating what it means, if you want to add a column with a NULL constraint to the table, the default value is fine. However, it seems that MySQL cannot give a default value to BLOB/TEXT type. When you check the migration file

users.rb


t.string :name,  null: false, default: ""
t.text :profile, null: false, default: nil  #← Here is """”, So changed

Resolved by changing the default value to "** nil **". When the engineer became stronger beyond the error, I felt like I read it somewhere, but I realized that feeling for the first time. It's refreshing to be able to solve it alone! I'll do my best tomorrow, too.

Recommended Posts

[Ruby] When adding a null constraint to a table
What to do when a null byte error occurs
[Rails] Processing after adding a column to the devise table
A validation error occurred when saving to the intermediate table.
How to add columns to a table
I get a Ruby version error when I try to start Rails.
[Rails] I learned about migration files! (Adding a column to the table)
What to do when a javax.el.PropertyNotWritableException occurs
How to give MAX + 1 ID to registered data when adding a new record
Ruby: CSV :: How to use Table Note
A note about adding Junit 4 to Android Studio
[Rails] How to write when making a subquery
I get an error when adding a dependency
[Ruby] I want to do a method jump!
(Ruby on Rails6) Creating data in a table
[Ruby] How to generate a random alphabet string
[Ruby] When you want to replace multiple characters
Introduction to Ruby 2
[Introduction] Try to create a Ruby on Rails application
Things to keep in mind when adding war to dependency
Things to watch out for when creating a framework
AtCoder Beginner Contest 170 A, B, C up to ruby
A memorandum when trying to create a GUI using JavaFX
How to launch another command in a Ruby program
What to do when rails creates a 〇〇 2.rb file
How to store Rakuten API data in a table
Ruby Regular Expression Extracts from a specific string to a string
Null value is entered when assigning to an array
A memo to check when you try to use Lombok
[Ruby on Rails] I get a warning when executing RSpec due to a different gem version.
[Ruby / Rails] What to do when NoMethodError appears when using a destructive method such as filter!