带有 yml 文件的 Rails DB 种子有时会删除并重新添加现有记录,有时则不会。什么给?

发布于 2024-08-22 09:50:08 字数 681 浏览 8 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如此安好 2024-08-29 09:50:08

我使用了一些不同的方法,但它对我来说始终有效。我没有使用 project.yml 文件,而是将种子数据加载到一个管道分隔的 txt 文件中,并将其放置在我创建的 db/seed_data/ 文件夹中,然后使用 find_or_create_by 加载数据,这样就不会覆盖现有数据。

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
...

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
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文