3

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.

Paulo Fidalgo
  • 21,709
  • 7
  • 99
  • 115
  • Possible duplicate of [Retrieve the nextval from a sequence using activerecord in Ruby on Rails 3.2.14 / Ruby 2.0.0 / PostgreSQL 9.2.4](https://stackoverflow.com/questions/17954524/retrieve-the-nextval-from-a-sequence-using-activerecord-in-ruby-on-rails-3-2-14) – Alex Suslyakov Dec 18 '18 at 12:08
  • @AlexeySuslyakov different scenarios, activerecord-import is used to populate large set of data, also this is not about gettting a sequence value, but instruct activerecord-import to generate the right SQL. – Paulo Fidalgo Dec 18 '18 at 12:29
  • oh, got it. Sorry then – Alex Suslyakov Dec 18 '18 at 13:55

1 Answers1

1

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) 
marsimus
  • 71
  • 7
  • Hi Marsmust. Can you provide some example code for your new solution? – tjheslin1 May 21 '20 at 16:21
  • 1
    sure, given I have position field with default sequence value: ` position | integer | not null default 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 ``` (SORRY, can't add formatting to the comment) – marsimus May 21 '20 at 17:58
  • That marsmust. Could you edit your answer to include this? There you will able to format it. – tjheslin1 May 22 '20 at 07:12