What is the correct way to add a boolean default that works in MySQL?

I did my migrations on my working server and I use MySQL, I get this error:

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

my migration is as follows:

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

I understand that the error is due to the fact that "false" is not the correct value for tinyint, in this case it should be 0. I thought that default :: false was the correct default way for boolean to false.

How to fix this, so MySQL is not complaining about a bad value?

+4
source share
2 answers

false not the character that I count. try it

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

PS I'm wrong. Therefore, you must set default: 0 :( Or you can install the ActiveRecord::Migration patch so that it accepts true|false

+8
source

This works in both PostgreSQL and MySQL:

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

I have not tried this with Ruby 1.9.2 syntax syntax, but I don't think this will be a problem.

0
source

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


All Articles