NULL value in CSV fixture, howto?

I am preparing some seed data for a deployment from generated a CSV
file. However, one of the model's columns ( child_id) needs to have a
null value in on a few of the rows where there is no Child instance.
Whatever I have put in there, it seems to just come in as a zero.

I have tried several combinations since I cannot find this documented
anywhere.

column1, child_id, column2
a,b gives |a|0|b|
a, null, b gives |a|0|b|
a, "", b gives |a|0|b|
a, nil, b gives |a|0|b

Any ideas?

Thanks.

O.

Owain wrote:

I am preparing some seed data for a deployment from generated a CSV
file. However, one of the model's columns ( child_id) needs to have a
null value in on a few of the rows where there is no Child instance.
Whatever I have put in there, it seems to just come in as a zero.

I have tried several combinations since I cannot find this documented
anywhere.

column1, child_id, column2
a,b gives |a|0|b|
a, null, b gives |a|0|b|
a, "", b gives |a|0|b|
a, nil, b gives |a|0|b

Check your table schema to see if child_id has a default value
configured.

create_table "domains", :force => true do |t|
    t.string "column1"
    t.integer "child_id"
    t.string "column2"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

so there is nothing in there but a good suggestion to check first.
Looks like 0 is the default value for integer and there is no way of
specifying NULL.

I can set up a workaround by adding another step in seeds to set all
of the 0 values to NULL since I do not have any defined child_id's of
0.

I will have a look in the Fixtures code to see what is happening on
this when I get the chance.

O.

Looks like the answer lies in CSV's method each_with_index

lib/active_record/fixtures.rb, line 720

    def read_csv_fixture_files
      reader = CSV.parse(erb_render(IO.read(csv_file_path)))
      header = reader.shift
      i = 0
      reader.each do |row|
        data = {}
        row.each_with_index { |cell, j| data[header[j].to_s.strip] =
cell.to_s.strip }
        self["#{@class_name.to_s.underscore}_#{i+=1}"] =
Fixture.new(data, model_class, @connection)
      end
    end

I will do some testing on CSV directly against the file and see what I
get once I have read the CSV documentation on null's and nils. If
that reads it ok then the problem lies somewhere in Fixture.new

Anyone else with any better idea?

Owain wrote:

Looks like the answer lies in CSV's method each_with_index

lib/active_record/fixtures.rb, line 720

    def read_csv_fixture_files
      reader = CSV.parse(erb_render(IO.read(csv_file_path)))
      header = reader.shift
      i = 0
      reader.each do |row|
        data = {}
        row.each_with_index { |cell, j| data[header[j].to_s.strip] =
cell.to_s.strip }
        self["#{@class_name.to_s.underscore}_#{i+=1}"] =
Fixture.new(data, model_class, @connection)
      end
    end

I will do some testing on CSV directly against the file and see what I
get once I have read the CSV documentation on null's and nils. If
that reads it ok then the problem lies somewhere in Fixture.new

Anyone else with any better idea?

Perhaps try using YAML, which has a specific, unambiguous syntax for
null values?

Best,