Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way of adding a default boolean value that works in MySQL?

I have ran my migrations on my production server and I am using MySQL, I get this error:

Mysql2::Error: Invalid default value for 'admin': ALTER TABLE users ADD admin tinyint(1) DEFAULT 'false'`

my migration looks like this:

class AddAdminToUsers < ActiveRecord::Migration
  def change
    add_column :users, :admin, :boolean, default: :false
  end
end

I understand the error is because "false" is not a proper value for a tinyint, this should be a 0 in this case. I thought default: :false was the right way to default a boolean to false.

How do I fix this so MySQL does not complain about the bad value?

like image 579
tdelam Avatar asked Mar 07 '12 16:03

tdelam


1 Answers

false is not a symbol I believe. Try this

add_column :users, :admin, :boolean, default: false

PS I am wrong. So you should set default: 0 :(. Or you can patch ActiveRecord::Migration so it will accept true|false

like image 53
fl00r Avatar answered Nov 15 '22 16:11

fl00r