How to make activerecord-import to use an sequence

566 views Asked by At

I have the following model:

                                                  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)

And I'm creating the objects using ActiveRecord, with:

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

What value should I pass to insights_id to use the models_insights_id_seq sequece? Tried DEFAULT and not passing anything and both fail to use the sequence, ie, making activerecord-import to generate nextval('public.models_insights_id_seq')

Note: This question is about to instruct activerecord-import to generate nextval('public.models_insights_id_seq') for the insights_id column, and not about using ActiveRecord to get the sequence next value.

1

There are 1 answers

3
marsimus On

Faced with same issue today. Just used another way for records import. Instead of importing of collection of AR objects, build a collection of attributes hashes without sequence parameters and then import them through Model.import. This way works for me.

Example. Given I have position column with default sequence value: position | integer | not null default nextval('file_exports.task_file_item_position_seq'::regclass)

In this code next sequence value will be set by Postgres automatically in each FileExports::TaskFileItem record.

... 
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)