Rails 3 SQLite3 Boolean false(Rails 3 SQLite3 布尔值 false)
问题描述
I'm trying to insert a false boolean value in a SQLite3 table but it always inserts a true value.
Here's my migration:
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.column :name, :string
t.column :active, :boolean, :default => false, :null => false
end
end
def self.down
drop_table :resources
end
end
When I try to insert using rails it produces the following SQL:
INSERT INTO "users" ("name", "active") VALUES ('test', 'f')
SQLite treats 'f' as true so it inserts true into my database. The query I want it to generate is:
INSERT INTO "users" ("name", "active") VALUES ('test', false)
What am I doing wrong?
rails: 3.0.7
sqlite3 gem: 1.3.3
SQLite uses 1 for true and 0 for false:
SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored as integers 0 (false) and 1 (true).
But SQLite also has a loose type system and automatically casts things so your 'f'
is probably being interpreted as having a truthiness of "true" simply because it isn't zero.
A bit of digging indicates that you have found a bug in the Rails 3.0.7 SQLiteAdapter. In active_record/connection_adapters/abstract/quoting.rb
, we find these:
def quoted_true
"'t'"
end
def quoted_false
"'f'"
end
So, by default, ActiveRecord assumes that the database understands 't'
and 'f'
for boolean columns. The MySQL adaptor overrides these to work with its tinyint
implementation of boolean columns:
QUOTED_TRUE, QUOTED_FALSE = '1'.freeze, '0'.freeze
#...
def quoted_true
QUOTED_TRUE
end
def quoted_false
QUOTED_FALSE
end
But the SQLite adapter does not provide its own implementations of quoted_true
or quoted_false
so it gets the defaults which don't work with SQLite's booleans.
The 't'
and 'f'
booleans work in PostgreSQL so maybe everyone is using PostgreSQL with Rails 3 or they're just not noticing that their queries aren't working properly.
I'm a little surprised by this and hopefully someone can point out where I've gone wrong, you can't be the first person to use a boolean column in SQLite with Rails 3.
Try monkey patching def quoted_true;'1';end
and def quoted_false;'0';end
into ActiveRecord::ConnectionAdapters::SQLiteAdapter
(or temporarily hand-edit them into active_record/connection_adapters/sqlite_adapter.rb
) and see if you get sensible SQL.
这篇关于Rails 3 SQLite3 布尔值 false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Rails 3 SQLite3 布尔值 false


基础教程推荐
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- 带更新的 sqlite CTE 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01