Rails 迁移未能在 MySQL 表中指定外键

发布于 2024-10-15 16:38:16 字数 1522 浏览 5 评论 0原文

 class CreateTestings < ActiveRecord::Migration
  def self.up
    create_table :testings do |t|
      t.string "name"
      t.boolean "visible"
      t.string "description"
      t.integer "roll"
      t.references "students"
      t.timestamps
    end
  end

  def self.down
     drop_table :testings
  end
end

你好,我刚刚运行了这个测试迁移来看看 Rails 如何处理迁移。尽管我有 t.references "students"

Rails 在我的测试表中成功创建了 students_id 但没有在其上指定任何外键:

mysql> DESC testings;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| name        | varchar(255) | YES  |     | NULL    |                |
| visible     | tinyint(1)   | YES  |     | NULL    |                |
| description | varchar(255) | YES  |     | NULL    |                |
| roll        | int(11)      | YES  |     | NULL    |                |
| students_id | int(11)      | YES  |     | NULL    |                |
| created_at  | datetime     | YES  |     | NULL    |                |
| updated_at  | datetime     | YES  |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+

这是 Rails 的工作方式还是我应该这样做已使用

t.references :student 而不是 t.references "students"

谢谢!

 class CreateTestings < ActiveRecord::Migration
  def self.up
    create_table :testings do |t|
      t.string "name"
      t.boolean "visible"
      t.string "description"
      t.integer "roll"
      t.references "students"
      t.timestamps
    end
  end

  def self.down
     drop_table :testings
  end
end

Hello, i just ran this test migration to see how Rails handles Migrations. Even though i have
t.references "students"

Rails created the students_id in my testings table successfully but however didn't specified any foreign key on it:

mysql> DESC testings;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| name        | varchar(255) | YES  |     | NULL    |                |
| visible     | tinyint(1)   | YES  |     | NULL    |                |
| description | varchar(255) | YES  |     | NULL    |                |
| roll        | int(11)      | YES  |     | NULL    |                |
| students_id | int(11)      | YES  |     | NULL    |                |
| created_at  | datetime     | YES  |     | NULL    |                |
| updated_at  | datetime     | YES  |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+

Is this how Rails works or otherwise i should've had

t.references :student instead of t.references "students"

Thanks!

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

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

发布评论

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

评论(1

°如果伤别离去 2024-10-22 16:38:16

这就是 Rails 的工作原理。它在迁移中没有指定外键依赖项。如果您确实想要指定外键依赖项,则必须使用“执行”命令和 SQL 代码手动执行此操作。

This is how rails works. It doesn't specify foreign key dependencies in its migrations. You'll have to do this manually with an 'execute' command and SQL code if you do want to specify foreign-key dependencies.

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