使用 rails 迁移创建缺少的自动增量属性 [英] Create missing auto increment attribute with rails migration
本文介绍了使用 rails 迁移创建缺少的自动增量属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写迁移以将非 Rails 应用程序转换为适用于 rails 的正确格式 - 其中一个表由于某种原因没有在 id 列上设置自动增量.有没有一种在迁移过程中打开它的快速方法,可能是 change_column
或其他什么?
I'm writing a migration to convert a non-rails app into the right format for rails - one of the tables for some reason does not have auto increment set on the id column. Is there a quick way to turn it on while in a migration, maybe with change_column
or something?
推荐答案
您需要执行一条 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)
注意这只是一个例子.最终的 SQL 语句语法取决于数据库.
Note this is just an example. The final SQL statement syntax depends on the database.
这篇关于使用 rails 迁移创建缺少的自动增量属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文