Note that there are some explanatory texts on larger screens.

plurals
  1. POActiverecord-import & serial column in PostgreSQL
    primarykey
    data
    text
    <p>I am in the process of upgrading a Rails 2.3.4 project to Rails 3.1.1. The old version used ar-extensions to handle a data import. I pulled out ar-extensions and replaced it with activerecord-import, which I understand has exactly the same interfaces.</p> <p>My code calls looks like this</p> <pre><code>Student.import(columns, values) </code></pre> <p>Both args are valid arrays holding the correct data, but I get a big fat error!</p> <p>The error stack looks like this:</p> <pre><code>NoMethodError (You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.split): activerecord (3.1.1) lib/active_record/connection_adapters/postgresql_adapter.rb:828:in 'default_sequence_name' activerecord (3.1.1) lib/active_record/base.rb:647:in `reset_sequence_name' activerecord (3.1.1) lib/active_record/base.rb:643:in `sequence_name' activerecord-import (0.2.9) lib/activerecord-import/import.rb:203:in `import' </code></pre> <p>Looking through the code it seems as though Activerecord-import calls activerecord which in turn looks for the name and next value of the Postgres sequence.</p> <p>So activerecord-import looks for the sequence_name lib/activerecord-import/import.rb:203</p> <pre><code># Force the primary key col into the insert if it's not # on the list and we are using a sequence and stuff a nil # value for it into each row so the sequencer will fire later -&gt; if !column_names.include?(primary_key) &amp;&amp; sequence_name &amp;&amp; connection.prefetch_primary_key? column_names &lt;&lt; primary_key array_of_attributes.each { |a| a &lt;&lt; nil } end </code></pre> <p>It calls active record ... lib/active_record/base.rb:647:in `reset_sequence_name'</p> <pre><code># Lazy-set the sequence name to the connection's default. This method # is only ever called once since set_sequence_name overrides it. def sequence_name #:nodoc: -&gt; reset_sequence_name end def reset_sequence_name #:nodoc: -&gt; default = connection.default_sequence_name(table_name, primary_key) set_sequence_name(default) default end </code></pre> <p>The code errors when <code>serial_sequence</code> returns nil and <code>default_sequence_name</code> tries to split it.</p> <pre><code>lib/active_record/connection_adapters/postgresql_adapter.rb # Returns the sequence name for a table's primary key or some other specified key. def default_sequence_name(table_name, pk = nil) #:nodoc: -&gt; serial_sequence(table_name, pk || 'id').split('.').last rescue ActiveRecord::StatementInvalid "#{table_name}_#{pk || 'id'}_seq" end def serial_sequence(table, column) result = exec_query(&lt;&lt;-eosql, 'SCHEMA', [[nil, table], [nil, column]]) SELECT pg_get_serial_sequence($1, $2) eosql result.rows.first.first end </code></pre> <p>When I execute <code>pg_get_serial_sequence()</code> directly against the database I get no value returned:</p> <pre><code>SELECT pg_get_serial_sequence('student', 'id') </code></pre> <p>But I can see that in the database there is a sequence called <code>student_id_seq</code></p> <p>I am using the following versions of Ruby, rails PG etc..</p> <ul> <li>Rails 3.1.1</li> <li>Ruby 1.9.2</li> <li>Activerecord-import 0.2.9</li> <li>pg 0.12.2</li> <li>psql (9.0.5, server 9.1.3)</li> </ul> <p>I have migrated the database from MySQL to PostgreSQL, I don't think this has any bearing on the problem but I thought that I'd better add it for completeness.</p> <p>I can't work out why this isn't working!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload