你能创建一个“临时”吗? Rails 迁移中的模型?

发布于 2025-01-03 22:37:32 字数 898 浏览 0 评论 0原文

我有一个 Rails 2 项目,它通过连接表具有多对多关系。我们将表称为 A、B 和 ABJ,其中 ABJ 具有属性 a_idb_id(以及与此问题无关的 id)和{创建,更新}_at)。

我想要做什么

不幸的是,这种关系从一开始就被错误地创建了,应该是简单的一对多(A has_many B's,B owns_to A)。因此,我创建了一个向上迁移,将 B 直接重新关联到 A。基本上是,1)将 a_id 添加到 B,2)对于每个 ABJ,将 abj.a.id 放入 abj.b.a_id,3)drop_table :abj。这很好用。

如果我需要 (1) create_table abj,2) 为每个 B 创建一个新的 abj,使得 abj.a_id = b,我还在向下迁移中创建了“逆”操作以返回。 a_id 和 abj.b_id = b.id, 3) 从 B) 中删除_列 a_id。这也很好用。

问题

除了将这种关系“重新关联”到一对多之外,还期望不再使用的连接资源 ABJ 将消失,即删除模型、控制器、测试等。问题是,如果我确实需要返回,运行向下迁移将不起作用,因为在第2步(对于每个B,创建一个新的abj)不再有任何ABJ类< ActiveRecord::Base 因为我删除了模型。

那么有没有办法在迁移中创建一个“临时”模型,只是为了操作数据库中的数据呢?或者您是否只是要求运行迁移的人员在运行之前需要确定该模型存在?因为如果向下迁移在步骤 2 失败,那么步骤 1 就已经创建了 abj 表,然后您必须手动删除它或注释掉再次运行迁移中的步骤 1 代码(然后取消注释)。

想知道是否有什么好的解决方案。

I have a Rails 2 project that has a many-many relationship through a join table. Let's call the tables A, B, and ABJ, where ABJ has properties a_id and b_id (along with irrelevant-to-this-question id and {created,updated}_at).

What I want to do

This relationship was unfortunately created incorrectly from the beginning, and should have been simply one-many (A has_many B's, B belongs_to A). So I created an up migration that reassociates the B's directly to the A's. Basically goes, 1) add_column a_id to B, 2) for every ABJ, put abj.a.id into abj.b.a_id, 3) drop_table :abj. This works fine.

I also created the "inverse" operation in the down migration to go back if I need to (1) create_table abj, 2) for every B, make a new abj such that abj.a_id = b.a_id and abj.b_id = b.id, 3) remove_column a_id from B). This works fine too.

Problem

Along with "re-associating" this relationship to a one-many is the expectation that the no-longer-used join resource ABJ will go away, i.e., deleting the model, controller, tests, etc. The problem is that if I do need to go back, running the down migration won't work because at step 2 (for every B, make a new abj) there is no longer any class ABJ < ActiveRecord::Base since I deleted the model.

So is there any way to make a "temporary" model within a migration just for the sake of manipulating data in the DB? Or do you simply require that the person running the migration needs to be certain that this model exists before running it? Because if the down migration fails at step 2, then step 1 would have already created the abj table, and then you'd have to go manually remove it or comment out the step 1 code in the migration before running it again (then uncomment it afterward).

Wondering if there is any nice solution to this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

愿与i 2025-01-10 22:37:32

您可以在迁移中定义模型。只需在迁移的顶部放置一个准系统定义:

class Pancake < ActiveRecord::Base; end

在删除表的情况下,您需要小心,当 pancakes 表不存在时,不要调用 Pancake 上的方法。

You can define a model in a migration. Simply put a barebones definition at the top of your migration:

class Pancake < ActiveRecord::Base; end

In a case where you're dropping a table you need to be careful that you're not calling methods on Pancake when the pancakes table doesn't exist.

酒废 2025-01-10 22:37:32

要么遵循 TomL 的建议,要么使用手动 SQL(假设您使用 MySQL):

UPDATE
  B, ABJ
SET
  B.a_id = ABJ.a_id
WHERE
  B.id = ABJ.b_id;

其他 RDBMS 允许使用 JOIN 的类似、更自然的语法。

Either go with TomL's advice or go with manual SQL (assuming you use MySQL):

UPDATE
  B, ABJ
SET
  B.a_id = ABJ.a_id
WHERE
  B.id = ABJ.b_id;

Other RDBMSes allow similiar, more natural syntax using JOINs.

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