Rails:从UUID到ID [英] Rails: From UUID to ID

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

问题描述

我已经创建了一个以UUID作为主键的模型Order. 现在,我想将其更改回普通的增量整数.

I've created a model Order with UUID as the primary key. Now, I'd like to change it back to a normal incremental integer.

我尝试过像这样的迁移:

I've tried to run a migration like:

add_column :orders, :id, :primary_key

但是它显然不起作用,因为已经有一个id列.

But it obviously doesn't work because there is already an id column.

我发现了很多关于如何从id转换为uuid的文章,但没有找到如何撤消它的文章.

I've found many posts on how to go from id to uuid, but not how to reverse it.

推荐答案

表中是否有数据?

如果否,只需删除旧的id列并添加新的:

If no, just drop old id column and add new:

remove_column :orders, :id
add_column :orders, :id, :primary_key

如果是,请将id列重命名为id_deprecated

If yes, rename id column to id_deprecated

rename_column :orders, :id, :id_deprecated
add_column :orders, :id, :primary_key

并添加逻辑以处理其他表中对old_id的引用.

and add your logic to handle references of old_id in other tables.

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

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