populator icon indicating copy to clipboard operation
populator copied to clipboard

Incorrect FK in nested models under Rails 3.2/PostgreSQL

Open ealden opened this issue 13 years ago • 1 comments

I am currently on Rails 3.2 with PostgreSQL as database, and the following example does not work as expected:

Foo.populate 10 do |foo|
    Bar.populate 10 do |bar|
        bar.foo_id = foo.id
    end
end

The entries for Bar should be linked to Foo through a foreign key. Instead the value of foo.id inside Bar seems to start from 0 always, thus, running a rake db:populate multiple times would cause the id of Foo to move forward (as it does not reset by default) and lead to Bar linking to the incorrect Foo.

This commit from route/populator@5c1da8c3784e6322c3d26f3b1c4511daeecb7a80 seems to fix this - might be a good idea to merge?

ealden avatar Jan 30 '12 16:01 ealden

Here is a temporary work-around for Postgre (discussed here: https://github.com/ryanb/populator/issues/18)

[Table1, Table2, Table3].each do |tbl|
  tbl.delete_all
  ActiveRecord::Base.connection.reset_pk_sequence!(tbl.table_name)
end

gmccue avatar Apr 11 '12 10:04 gmccue