【问题标题】:How to make activerecord-import to use an sequence如何使 activerecord-import 使用序列
【发布时间】:2019-05-18 20:16:57
【问题描述】:

我有以下型号:

                                                  Table "public.models"
        Column        |            Type             | Collation | Nullable |                   Default                   
----------------------+-----------------------------+-----------+----------+---------------------------------------------
 id                   | bigint                      |           | not null | nextval('models_id_seq'::regclass)
 research_provider_id | bigint                      |           | not null | 
 covered_company_id   | bigint                      |           | not null | 
 publication_date     | timestamp without time zone |           | not null | 
 created_at           | timestamp without time zone |           | not null | 
 updated_at           | timestamp without time zone |           | not null | 
 insights_id          | bigint                      |           | not null | nextval('models_insights_id_seq'::regclass)
Indexes:
    "models_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
    "fk_rails_22d32db7ac" FOREIGN KEY (covered_company_id) REFERENCES companies(id)
    "fk_rails_3a764bb9c1" FOREIGN KEY (research_provider_id) REFERENCES companies(id)
Referenced by:
    TABLE "model_product_groups" CONSTRAINT "fk_rails_1866a14ba0" FOREIGN KEY (model_id) REFERENCES models(id)
    TABLE "model_analysts" CONSTRAINT "fk_rails_c7730c705b" FOREIGN KEY (model_id) REFERENCES models(id)

我正在使用 ActiveRecord 创建对象,其中:

   Model.new(
        # insights_id: 
        research_provider_id: company.id,
        covered_company_id: covered_company_id,
        publication_date:  Time.current - rand(1..20).day,
    ......
   )

我应该将什么值传递给insights_id 以使用models_insights_id_seq 序列?试过DEFAULT,没有传递任何东西,都无法使用序列,即进行activerecord-import生成nextval('public.models_insights_id_seq')

注意:这个问题是要指示activerecord-importinsights_id 列生成nextval('public.models_insights_id_seq'),而不是使用ActiveRecord 来获取序列下一个值。

【问题讨论】:

标签: ruby-on-rails ruby activerecord-import


【解决方案1】:

今天面临同样的问题。只是使用另一种方式进行记录导入。与其导入 AR 对象集合,不如构建一个不带序列参数的属性哈希集合,然后通过Model.import 导入它们。这种方式适合我。

示例。鉴于我有默认序列值的位置列: position | integer | not null default nextval('file_exports.task_file_item_position_seq'::regclass)

在这段代码中,下一个序列值将由 Postgres 自动设置 每个FileExports::TaskFileItem 记录。

... 
params = file_records.map do |file_record|
  build_file_item_params(file_record) 
end 

def build_file_item_params(file_record) 
  { 
    name: "some_name", 
    link: file_record.file.url 
  } 
end 

FileExports::TaskFileItem.import!(params, validate: true) 

【讨论】:

  • 嗨,Marsmust。您能为您的新解决方案提供一些示例代码吗?
  • 当然,因为我有默认序列值的位置字段:` position |整数 |不为空 默认 nextval('file_exports.task_file_item_position_seq'::regclass) ` ``` ... params = file_records.map do |file_record| build_file_item_params(file_record) end FileExports::TaskFileItem.import!(params, validate: true) def build_file_item_params(file_record) { name: "some_name", link: file_record.file.url } end ``` (抱歉,无法添加格式化评论)
  • 那个marsmust。你能编辑你的答案来包括这个吗?在那里你可以格式化它。
猜你喜欢
  • 1970-01-01
  • 2012-04-01
  • 2011-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-08
  • 2015-12-22
  • 1970-01-01
相关资源
最近更新 更多