在 Rails 中重置主键 ID [英] reseting primary key id in rails

查看:32
本文介绍了在 Rails 中重置主键 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从表中删除_all 并再次开始插入时,我遇到了主键如何递增的问题.这是有问题的,因为我有与清除的表相关联的其他模型.例如,如果我有属于 Computer 的 Apple,那么当我清除 Computer 表并重新添加计算机时,关联会变得混乱,因为 Apple 现在在其行中有婴儿 computer_id.我希望能够重置计算机的主键,以便当我重新添加计算机时,关联仍然保持不变.我应该如何在 Rails 中执行此操作?

I am having problem with how the primary key gets incremented when I delete_all from a table and start inserting again. This is problematic because I have other models that are associated with the purged table. For example, if I have Apple that belongs_to Computer, then when I purge the Computer table and re-add the computer, the associations get messed up because Apple now has infant computer_id in its rows. I want to be able to reset the primary key of the computer so that when I re-add the computer, the association still stays intact. How should I go about doing this in rails?

编辑我很抱歉没有说清楚这一点,但现在我会告诉你我想做的事.我希望用户与机器(计算机)相关联.一台电脑有自己的id,只能由管理员创建.电脑桌有固定数量的电脑.

Edit I am very sorry for not being clear about this, but now I will tell you want I want to do. I want a user to be associated with a machine (computer). A computer has his own id and can only be created by the admin. There are a fixed amount of computer in the computer table.

User
belongs_to :computer

Computer
has_many :users

我面临的问题是,我在seeds.rb/some rakefile 中预加载了计算机表.当我启动我的应用程序时,我只想运行首先删除所有内容然后预加载表的文件.这将让用户仅从计算机表中提供的计算机中选择计算机.我希望能够再次运行这个脚本(比如在 heroku 控制台中,不删除 User 表)并且仍然获得与每台机器(以及每个用户)相关联的相同 ID.我很困惑我应该如何做到这一点.请告诉我是否应该以不同的方式执行此操作.

The problem that I am facing is that, I have the computer table pre-loaded inside seeds.rb/some rakefile. When I start up my app, I want to just run the file that first deletes everything and then pre-loads the table. This will let the user choose a computer only from the ones provided in the computer table. I want to be able to run this script again (say in heroku console, without dropping the User table) and still get the same id associated to each machine (and thus to each user). I am confused on how I should do this. Please let me know if I should do this differently.

推荐答案

首先,你不应该这样做!如果要更改记录,只需更新表中的行.不要删除它并重新插入它!如果你还想这样做,那么你应该删除其他表中所有关联的行,同时插入计算机和苹果.

First, you shouldn't do it! If you want to change record, then just update rows in your table. Don't delete it and reinsert it! If you still want to do it, then you should delete all associated rows in other tables and insert both computers and apples.

我不知道 Rails 重置自动增量主键的方法,但是如果您使用 mysql 那么您可以运行自定义查询:

I don't know Rails way of reseting auto increment primary key, but if you use mysql then you can run custom query:

ActiveRecord::Base.connection.execute('ALTER TABLE tablename AUTO_INCREMENT = 1')

它应该将自动增量值重置为下一个可用数字.

it should reset auto increment value to next available number.

如何使用 id 加载数据.我不确定加载种子是否可以做到这一点,但使用固定装置肯定会有所帮助.您需要准备带有数据的 computers.yml 文件:

How to load data with id. I'm not sure if loading with seeds can do it, but for sure using fixtures can help. What you need is to prepare computers.yml file with data:

First_computer:
  id: 1
  name: computer

Second_computer:
  id: 2
  name: dell

然后你可以用它来加载它到数据库:

Then you can use this to load it to db:

require 'active_record/fixtures'

Fixtures.create_fixtures("/path/to/directory/where/your/yml/file/is/", "computers")

但它会重置整个表(所有行都将被删除并重新插入 - 但您可以保留您的 ID).

But it will reset whole table (all rows will be deleted and reinserted - but you can keep your ids).

再次提醒您,以这种方式加载数据是个坏主意.Fixtures/seeds 应该只用于用初始(需要启动您的应用程序)数据为您的数据库播种.

Again I want to warn you that it is bad idea to load data this way. Fixtures/seeds should be used only to seed your db with initial (needed to start your application) data.

为什么要重置此表?

这篇关于在 Rails 中重置主键 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆