Rails 3 migrations:布尔(mysql vs postgreSQL) [英] Rails 3 migrations: boolean (mysql vs postgreSQL)

查看:92
本文介绍了Rails 3 migrations:布尔(mysql vs postgreSQL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的论坛主题上添加一个粘性选项。这是我的迁移过程的样子。

  def self.up 
add_column:topics,:sticky,:boolean,: null => false,:default => false
end

def self.down
remove_column:topics,:sticky
end

这对于mysql本地工作非常完美,但是当我将更改推送到heroku(使用PostgreSQL)时,这就是我在使用控制台时得到的结果

 >> t.sticky 
=> f
>> t.sticky.class
=>字串
>> t.sticky = true
=> true
>> t.sticky.class
=> TrueClass

为什么这个属性的默认值是String?



编辑:
如果我保存对象,它不会更改sticky属性,即它仍然是f。

解决方案

在psql中,布尔值显示为 t f 。根据数据库驱动程序的不同,这些数据会转换为布尔值或保留为字符串表示形式。



PHP中的PDO驱动程序的功能相同。 (或者习惯了,无论如何......我隐约记得它不再在它的最新版本中)。

I'm trying to add a "sticky" option on my forum topics. This is how my migration looks like

  def self.up
    add_column :topics, :sticky, :boolean, :null => false, :default => false
  end

  def self.down
    remove_column :topics, :sticky
  end

This works perfect locally on mysql, but when I push the changes to heroku (which uses PostgreSQL), this is what I get when using the console

>> t.sticky
=> "f"
>> t.sticky.class
=> String
>> t.sticky = true
=> true
>> t.sticky.class
=> TrueClass

Why is the default value of this property a String?

Edit: And if I save the object, it doesn't change the sticky property, i.e. it's still "f".

解决方案

In psql, booleans are displayed as t or f. Depending on the DB driver, these get converted to booleans or left in their string representation.

The PDO driver in PHP does the same thing. (Or used to, anyway... I vaguely recall it no longer does in its latest version.)

这篇关于Rails 3 migrations:布尔(mysql vs postgreSQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆