带有 yml 文件的 Rails DB 种子有时会删除并重新添加现有记录,有时则不会。什么给?
我有一个 Rails 项目,其中的种子是使用 rake db:seed 执行的。这反过来会加载 db/seeds 目录中的 RB 文件,该文件又会执行如下所示的操作:
Fixtures.create_fixtures("db/seeds","projects")
表单中有一个projects.yml
project_name1:
property: value
project_name2:
property: value
在projects SQL 表中存在现有的projects 记录。 YML 文件中的一些是新的,而另一些则不是。我花了几天时间运行数据库种子,它会改变一些项目 ID,但不会改变其他项目 ID。但它不会复制那些未更改 ID 的内容,即使它们都在 YML 文件中。因此,有些记录是可以接受的,而另一些记录则会删除并使用新 ID 重新添加(或者只是直接更新 ID,不确定是哪一个)。
然后突然它停止这样做了。我像平常一样删除并重新加载数据库(使用 sql 转储恢复到干净、未更改的状态),但数据库种子运行完美,保留现有数据,仅添加新数据(即使所有数据都在 yml 文件中)而不触及现有的 ID。
然后突然,它又开始这样做了。我花了两周时间在谷歌上搜索有关种子、现有数据播种和 ID 更新的任何内容,但没有成功。
任何帮助当然不胜感激。
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我使用了一些不同的方法,但它对我来说始终有效。我没有使用 project.yml 文件,而是将种子数据加载到一个管道分隔的 txt 文件中,并将其放置在我创建的 db/seed_data/ 文件夹中,然后使用 find_or_create_by 加载数据,这样就不会覆盖现有数据。
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.