向 Ruby on Rails (MySQL) 中的现有表添加外键约束

发布于 2024-08-28 18:16:37 字数 150 浏览 6 评论 0原文

使用底层 MySQL 数据库向 Rails 中的现有表添加外键的最佳方法是什么?显然,解决方案应该在迁移中完成,因为我想要这个版本。否则我会自己创造约束。

我似乎找不到对上述问题有利的回应。同样,这些表已经通过之前的迁移创建了。我现在要回去并在适用的地方添加引用完整性。

What's the best way to add foreign keys to my existing tables in Rails with an underlying MySQL database? clearly the solution should be done in a migration, as I want this versioned. Otherwise I'd create the constraints myself.

I can't seem to find one, conducive response to they above. Again, the tables have already been created with previous migrations. I'm just going back now and adding referential integrity wherever it's applicable.

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

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

发布评论

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

评论(1

伴梦长久 2024-09-04 18:16:37

Foreigner 非常适合管理外键。

或者,您可以仅使用execute 方法在新迁移中发出ALTER 语句。

Foreigner works nicely for managing foreign keys.

Alternatively, you can just use the execute method to issue ALTER statements within your new migrations.

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