I have a rails project with seeds that are executed using rake db:seed. This in turn loads the RB files in the db/seeds directory which in turn executes something like this:
Fixtures.create_fixtures("db/seeds","projects")
There is a projects.yml in the form
project_name1:
property: value
project_name2:
property: value
In the projects SQL table there are existing projects records. Some of the ones in the YML file are new and others are not. I spent several days running DB seeds and it would change some of the project IDs but not others. But it wouldn't duplicate the ones that it didn't change the ID of even though all of them are in the YML file. So some of the records it was okay with, others it would remove and re-add with a new id (or just update the ID outright, not sure which).
Then suddenly it stopped doing this. I drop and reload my database as normal (using sql dumps to get back to a clean, unaltered state) but 开发者_Go百科DB seeds runs perfectly leaving the existing data alone and only adding the new data (even though all of it is in the yml file) without touching the existing IDs.
Then suddenly again, it started doing it again. I've spent two weeks searching google for anything on seeds, existing data seeding and ID updating with no luck.
Any help is of course appreciated.
I used a little different approach, but its worked consistently for me. Rather than using a project.yml file, I loaded my seed data in a pipe delimited txt file I placed in a db/seed_data/ folder I created, then used find_or_create_by to load the data, so that it wouldn't overwrite existing data.
seed.rb
directory = "db/seed_data/"
# Pre-load
path = File.join(directory, "projects.txt")
open(path) do |projects|
projects.read.each_line do |project|
name, owner = school.chomp.split("|")
Project.find_or_create_by_name_and_owner!(name, owner)
end
end
----------------
projects.txt
Project A|admin
Project B|user1
Project C|admin
...
精彩评论