在 Ruby on Rails 中自动增加非主键字段 [英] Auto increment a non-primary key field in Ruby on Rails

查看:15
本文介绍了在 Ruby on Rails 中自动增加非主键字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 RoR 迁移中,如何自动递增非主键字段?我想在 db 定义中执行此操作,而不是在模型中.

In a RoR migration, how do I auto increment a non-primary-key field? I'd like to do this in the db definition, and not in the model.

推荐答案

你需要执行一条SQL语句.

You need to execute an SQL statement.

statement = "ALTER TABLE `users` CHANGE `id` `id` SMALLINT( 5 ) UNSIGNED NOT NULL AUTO_INCREMENT" 
ActiveRecord::Base.connection.execute(statement)

您可以在迁移中手动输入

you can entry manually in your migration

请注意,这只是一个示例.最终的 SQL 语句语法取决于数据库.

Note this is just an example. The final SQL statement syntax depends on the database.

这篇关于在 Ruby on Rails 中自动增加非主键字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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