如何在Rails中更改记录的主要ID? [英] How to change primary ID of a record in Rails?

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

问题描述


rails console
u = User.find(9)
u.id = 7 # There is no other record with id 7
u.save
=> true
User.all

ID不变。

如何更改主要ID?为什么它阻止了此操作?

How to change the primary ID? Why does it prevent this action?

在Rails 3.0.7和PostgreSQL中工作。

Working in Rails 3.0.7 and PostgreSQL.

编辑:
由于有充分的理由不这样做,所以我将解释原因,并希望这是一个很好的理由。

Since there are good reasons not to do this, I'll explain why and hopefully it is a good reason.

我们正在对登台进行可用性测试,所以我希望它看起来像正式版,以便所有人都可以轻松使用。我不想在可用性测试过程中造成混乱或错误,因为某些事情在登台中按一个顺序排列,而在生产中按不同的顺序排列。我只在登台数据库上更改了一个PK ID。

We are doing Usability Testing on Staging, so I want it to look like the Production to make it easy for everyone. I don't want to create confusion or errors in the Usability Testing process by having some things in one order on Staging and in a different order on Production. I only changed one PK id on Staging DB.

请勿在生产数据库上执行此操作!

Don't do this on your production DB!

推荐答案

我不确定为什么它阻止了该操作,但是确实可以阻止它。

I'm not sure as to why it prevents that action, but it does for sure prevent it.

您可以为用户使用update_all方法绕过此操作。

You can bypass this using the update_all method on for the user.

User.where(id: 7).update_all(id: 9)

尽管有可能,您应该避免这样做。

Though if possible, you really should avoid doing this.

这篇关于如何在Rails中更改记录的主要ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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