Ruby on rails ActiveRecord中的自定义数据库类型

Ruby on rails ActiveRecord中的自定义数据库类型,ruby-on-rails,activerecord,earthdistance,Ruby On Rails,Activerecord,Earthdistance,我正在使用Rails 3.1.1和PostgreSQL 9.1以及。 为了能够正确计算不同位置之间的距离,我在分支表中设置了一个带有地球类型的列 我现在遇到的问题是,使用此表的Rails应用程序不了解地球类型,因此我在我的db/schema.rb: # Could not dump table "branches" because of following StandardError # Unknown type 'earth' for column 'location' #由于以下错误,无

我正在使用Rails 3.1.1和PostgreSQL 9.1以及。 为了能够正确计算不同位置之间的距离,我在
分支
表中设置了一个带有
地球
类型的列

我现在遇到的问题是,使用此表的Rails应用程序不了解地球类型,因此我在我的
db/schema.rb

# Could not dump table "branches" because of following StandardError # Unknown type 'earth' for column 'location' #由于以下错误,无法转储表“分支” #列“位置”的未知类型“接地” 这是有问题的,因为现在我无法从schema.rb创建测试数据库

如何将此类型添加到AR或使其忽略该列?尝试以下操作:

更改您的配置/application.rb

config.active_record.schema_format = :sql

这会将输出格式更改为本机PostgreSQL格式,schema.rb将被禁用,并将生成一个新文件/db/config/structure.SQL

将自定义postgres类型(枚举)插入PostgreSQL时可能会遇到这种情况。如果执行此操作后仍希望在不使用sql转储程序的情况下转储schema.rb,则可以将自定义数据库类型添加到适配器的有效类型列表中

例如,假设您有一个
客户
,具有
状态
优先级
,并且希望使用postgres的本机枚举类型。您需要在迁移过程中手动进行一些设置,并且需要一个初始值设定项来帮助Rails生成模式

迁移 此解决方案的验证 这是您将在数据库中看到的内容

enum_in_db_development=# INSERT INTO customers VALUES (1, 'Mario','active','high');
INSERT 0 1
enum_in_db_development=# INSERT INTO customers VALUES (1, 'Mario','active','notthere');
ERROR:  invalid input value for enum type_priority: "nothere"
LINE 1: INSERT INTO customers VALUES (1, 'Mario','active','notthere')
                                                          ^
enum_in_db_development=# SELECT enum_range(NULL::type_priority);
    enum_range     
-------------------
 {high,medium,low}
(1 row)
关于我的解决方案的注意事项:

  • 我正在检查
    PostgreSQLAdpater
    的存在性,因为我使用的静态分析gem部分加载了一些AR依赖项,特别是gem
    注释
  • 本机\u数据库\u类型的原始定义的源

我遇到这种情况的背景:当我在rails 5.0.x中遇到这种情况时,迁移运行正常,但当我尝试运行
db:test:prepare
db:reset
时,测试环境将失败。我花了很长时间才找到模式转储问题。

对于任何访问此页面的人,有人已经在一个易于使用的gem中实现了这一点

这分别是迁移文件和模型文件的外观

# migration
class CreatePerson < ActiveRecord::Migration
  def change
    create_enum :mood, %w(happy great never_better)

    create_table :person do |t|
      t.enum :person_mood, enum_name: :mood
    end
  end
end

# model
class Person < ActiveRecord::Base
  enum mood: { happy: 'happy', great: 'great', never_better: 'never_better' }
end
#迁移
类CreatePerson

在这里找到了问题的答案:写下自己问题的答案并接受它对其他人很有帮助。它还将这个问题从未回答的列表中删除。我确实尝试过这个,但是rails
db:schema:load
似乎仍然在查找
schema.rb
文件。看起来您必须使用
rake db:structure:load
。这在rails 4.2.8中对我不起作用,我还需要使用其他公式吗?你能分享更多关于你遇到的错误的信息吗?也许把你的代码放在一个要点中,让我看看吗?我完全按照你键入的内容运行,但它在5.0.1版本中不起作用。。。我没有任何问题,我是我的项目,但我也编辑了以上更一般。如果你需要帮助,你能说得更具体些吗。到底什么东西没有运行?是否有错误消息?当其他开发人员试图帮助您时,请让他们轻松一些。模式转储可以工作,但实际列不是PG枚举。相反,它是一个常规的
字符变化
,因此您可以插入任何您喜欢的内容,因为没有枚举数据完整性检查:(
enum_in_db_development=# INSERT INTO customers VALUES (1, 'Mario','active','high');
INSERT 0 1
enum_in_db_development=# INSERT INTO customers VALUES (1, 'Mario','active','notthere');
ERROR:  invalid input value for enum type_priority: "nothere"
LINE 1: INSERT INTO customers VALUES (1, 'Mario','active','notthere')
                                                          ^
enum_in_db_development=# SELECT enum_range(NULL::type_priority);
    enum_range     
-------------------
 {high,medium,low}
(1 row)
# migration
class CreatePerson < ActiveRecord::Migration
  def change
    create_enum :mood, %w(happy great never_better)

    create_table :person do |t|
      t.enum :person_mood, enum_name: :mood
    end
  end
end

# model
class Person < ActiveRecord::Base
  enum mood: { happy: 'happy', great: 'great', never_better: 'never_better' }
end