在表视图的ActiveRecord / sqlite3的列类型失去了什么? [英] ActiveRecord/sqlite3 column type lost in table view?

查看:308
本文介绍了在表视图的ActiveRecord / sqlite3的列类型失去了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的ActiveRecord的测试用例,模仿我的问题。我有一个表的人有一个属性是一个日期。我创建一个视图对该表添加一列这只是日期加20分:

I have the following ActiveRecord testcase that mimics my problem. I have a People table with one attribute being a date. I create a view over that table adding one column which is just that date plus 20 minutes:

#!/usr/bin/env ruby

%w|pp rubygems active_record irb active_support date|.each {|lib| require lib}

ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => "test.db"
)

ActiveRecord::Schema.define do
  create_table :people, :force => true do |t|
    t.column :name, :string
    t.column :born_at, :datetime
  end

  execute "create view clowns as select p.name, p.born_at, datetime(p.born_at, '+' || '20' || ' minutes') as twenty_after_born_at from people p;"

end

class Person < ActiveRecord::Base
  validates_presence_of :name
end

class Clown < ActiveRecord::Base
end

Person.create(:name => "John", :born_at => DateTime.now)

pp Person.all.first.born_at.class
pp Clown.all.first.born_at.class
pp Clown.all.first.twenty_after_born_at.class

的问题是,产量

The problem is, the output is

Time
Time
String

当我想到鉴于新的日期时间属性是也是在红宝石世界时间或日期时间。任何想法?

When I expect the new datetime attribute of the view to be also a Time or DateTime in the ruby world. Any ideas?

我也试过:

create view clowns as select p.name, p.born_at, CAST(datetime(p.born_at, '+' || '20' || ' minutes') as datetime) as twenty_after_born_at from people p;

使用相同的结果。

推荐答案

好了,基本上没有,而不是MySQL的SQLite中没有DATATIME类型。在您的例子中,你明确地定义类型的表,但没有指定类型的视图。这可能是问题。无法检查它,因为我从来没有触及红宝石。

Well, basically there is no datatime type in SQLite as opposed to MySQL. In your example you explicitly define types for the table but do not specify types for the view. That might be the problem. Can not check it since I have never touched ruby.

这篇关于在表视图的ActiveRecord / sqlite3的列类型失去了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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