Sequal join and insert (Ruby) - Joined datasets cannot be modified (Sequel::InvalidOperation)

29 views Asked by At

I have two postgresql tables with the following structures

table samples

id sample_date location_id value
1 2023-08-02 0213 0.57
2 2023-08-02 0214 0.52

table locations

id treatment replicate
0213 A 1
0214 B 1

Now, I have a dataset in an excel spreadsheet that I would like to load to table samples. The location_id and sample_date are non-null values (constrains). I need to match treatment and replcate between the excel spreadsheet and the postgres table locations, and insert sample_date, location_id and value to table locations.

   require 'logger'
   #require 'simple_xlsx_reader'
   require 'yaml'
   require 'date'
   require 'simple_xlsx_reader'

   credential_file = '/Users/credential_file.yaml'
   credentials = File.open(credential_file) { |y| YAML.safe_load(y) }


   PR = Sequel.postgres(database: '',
                    host: '',
                    user: credentials['username'],
                    loggers: [Logger.new($stdout)],
                    password: credentials['password'])

   samples= PR[Sequel.qualify('public', 'samples')]
   locs = PR[Sequel.qualify('public', 'locs')]

   PR.transaction do
   workbook = Creek::Book.new 'data.xlsx'
   worksheets = workbook.sheets
   worksheets[0].rows.drop(1).each do |row|

    sample_date = row.values[0]
    treatment = row.values[3]
    replicate = row.values[4]
    value = row.values[5]

    samples.join(:inner, locs).insert(sample_date: sample_date, location_id: 0214,
    value: value)


    end

    raise done
end

Error message appears: `_check_modification_allowed!': Joined datasets cannot be modified (Sequel::InvalidOperation)

I also don't know how to match ids in locations table. Many thanks.

0

There are 0 answers