-1

I wanted to start writing tests for my application, but I get this error from PG.

I have no public table and no increment_by column.

Why is this error occurring?

PG::UndefinedColumn

RomanOks
  • 692
  • 3
  • 13
  • Please provide model, controller and test code so that we can help you – Amir El-Bashary Nov 14 '19 at 08:33
  • @AmirEl-Bashary This happens with any action in test environment. My test now consists of one line. `visit root_path`. Capybara. – RomanOks Nov 14 '19 at 08:51
  • Are you sure the test DB is created and migrated ? try running `RAILS_ENV=test rails db:create` to make sure it's created then try migrate instead of create to make sure it's migrated – Amir El-Bashary Nov 14 '19 at 08:54
  • After that try `rails c -e test` then `ActiveRecord::Base.establish_connection` then `ActiveRecord::Base.connection` then `ActiveRecord::Base.connected?` make sure you get `true` to make sure nothing wrong with DB connection, after that try doing any model query or just initialize new object as a test `Model.new` where Model can be any of your model created on the app – Amir El-Bashary Nov 14 '19 at 09:02
  • @AmirEl-Bashary Checked in test console, I get `true`. But I can't create an object, Rollback returns. – RomanOks Nov 14 '19 at 09:17
  • try Model.create! with exclamation mark to get the error and let me know what you got.. if the same error shows above then i'll need to see you rspec configs files also please provide ruby/rails versions – Amir El-Bashary Nov 14 '19 at 09:33
  • @AmirEl-Bashary Turns out I accidentally inserted another symbol. Object is created without problems. `Ruby -v 2.3.5` `Rails -v 4.2.6`. I don't use `rspec`. – RomanOks Nov 14 '19 at 09:42
  • So your problem is solved ? – Amir El-Bashary Nov 14 '19 at 09:43
  • @AmirEl-Bashary No, I still can’t run the tests. – RomanOks Nov 14 '19 at 09:49
  • check if your pg schema does not contain this `public table and no increment_by columns`. Run `rails dbconsole -e test` and then `\dt` it will show you your current schema – nuaky Nov 14 '19 at 11:14
  • @nuaky Did as you said, but no, there is no `public` table. – RomanOks Nov 14 '19 at 11:20
  • @RomanOks did you try this solution? https://github.com/rails/rails/issues/28780#issuecomment-354868174 it's for Rails 4.2 and Postgresql 10 – nuaky Nov 14 '19 at 12:13

1 Answers1

0

Ref https://github.com/rails/rails/issues/28780#issuecomment-354868174

Put it into an initializer at config/initializers/pg_10_support_rails_4.rb, and all is well:

require 'active_record/connection_adapters/postgresql/schema_statements'

#
# Monkey-patch the refused Rails 4.2 patch at https://github.com/rails/rails/pull/31330
#
# Updates sequence logic to support PostgreSQL 10.
#

module ActiveRecord
  module ConnectionAdapters
    module PostgreSQL
      module SchemaStatements
        # Resets the sequence of a table's primary key to the maximum value.
        def reset_pk_sequence!(table, pk = nil, sequence = nil) #:nodoc:
          unless pk and sequence
            default_pk, default_sequence = pk_and_sequence_for(table)

            pk ||= default_pk
            sequence ||= default_sequence
          end

          if @logger && pk && !sequence
            @logger.warn "#{table} has primary key #{pk} with no default sequence"
          end

          if pk && sequence
            quoted_sequence = quote_table_name(sequence)
            max_pk = select_value("SELECT MAX(#{quote_column_name pk}) FROM #{quote_table_name(table)}")
            if max_pk.nil?
              if postgresql_version >= 100000
                minvalue = select_value("SELECT seqmin FROM pg_sequence WHERE seqrelid = #{quote(quoted_sequence)}::regclass")
              else
                minvalue = select_value("SELECT min_value FROM #{quoted_sequence}")
              end
            end

            select_value <<-end_sql, 'SCHEMA'
              SELECT setval(#{quote(quoted_sequence)}, #{max_pk ? max_pk : minvalue}, #{max_pk ? true : false})
            end_sql
          end
        end
      end
    end
  end
end
RomanOks
  • 692
  • 3
  • 13