Tag: rails migrations

Rails 4.将表id迁移到UUID

我有一个表: db / migrate / 20140731201801_create_voc_brands.rb: class CreateVocBrands < ActiveRecord::Migration def change create_table :voc_brands do |t| t.string :name t.timestamps end end end 但我需要将表更改为此(如果我将从零创建它): class CreateVocBrands false do |t| t.uuid :id, :primary_key => true t.string :name t.timestamps end add_index :voc_brands, :id end end 如何使用迁移更改此设置?

推送到Heroku时Rails迁移错误w / Postgres

我正在尝试执行以下向上迁移以更改“tweet”模型表中的“number”列 class ChangeDataTypeForTweetsNumber < ActiveRecord::Migration def up change_column :tweets do |t| t.change :number, :integer end end def down change_table :tweets do |t| t.change :number, :string end end end 执行以下向上迁移到heroku …. heroku rake db:migrate:up VERSION=20120925211232 我收到以下错误 PG::Error: ERROR: column “number” cannot be cast to type integer : ALTER TABLE “tweets” ALTER COLUMN “number” TYPE integer 任何想法都会非常感激。 […]

在rails中生成自动增量字段

我有一个模型令牌,它有一个字段token_number,我需要自动递增(从1001开始),当且仅当用户不提供它。 问题是,由于用户可以选择提供此字段,因此无法完全查询数据库并请求最大的token_number。 我在这个论坛上找到了一个答案,但我确信必须有一个更好的方法来执行它而不是执行SQL语句? 在Ruby on Rails中自动增加非主键字段

Rails rake db:migrate没有任何影响

我今天制作了一个新的Rails 3应用程序,添加了一个简单的迁移,并且出于某种原因,当我执行rake db:migrate时没有任何反应。 它只是暂停几秒钟,然后返回到命令提示符,没有任何错误或任何错误。 Schema.rb和数据库保持空白。 有什么想法可能会发生什么? 我做了很多应用程序,从来没有遇到过这个问题。 一切都是完全标准的设置。

Ruby on Rails上“预期的字符串默认值为…”的含义

最近我在Rails(5.0.0.1)上为Ruby(2.3.3)创建了一个应用程序: $ rails _5.0.0.1_ new myapp –database=postgresql -T 设置Gemfile并测试与我的数据库的连接后: $ rails db:migrate 我试图生成模型,但我收到了奇怪的消息: $ rails g model Competition title:string Expected string default value for ‘–test-framework’; got false (boolean) Expected string default value for ‘–jbuilder’; got true (boolean) Expected string default value for ‘–test-framework’; got false (boolean) invoke active_record create db/migrate/20161206021603_create_competitions.rb create app/models/competition.rb 这些消息的含义是什么? “预期的字符串默认值为…” ? […]

PG :: UndefinedTable:错误:关系“…”不存在

在迁移时,我收到以下错误消息: PG::UndefinedTable: ERROR: relation “actioncodes” does not exist : ALTER TABLE “organizations” ADD CONSTRAINT “fk_rails_4ecaa2493e” FOREIGN KEY (“actioncode_id”) REFERENCES “actioncodes” (“id”) 我有组织的以下迁移文件: class CreateOrganizations < ActiveRecord::Migration def change create_table :organizations do |t| t.string :name, null: false, limit: 40 t.references :actioncode, index: true, foreign_key: true t.boolean :activated t.datetime :activated_at t.timestamps null: false end end end 对于Actioncodes,我有迁移文件: […]

Rails:如何迁移我添加了:belongs_to关系的数据库?

这是我的第一个rails应用程序,我创建的唯一目的是学习rails。 我创建了一个应用程序,其中包含用户和产品(以及会话,但这与此无关)。 在做了rake db:mograte创建一些项目并进行测试后,我想添加一个关系,其中产品belongs_to:users和users has_many:products。 但是,当然,因为我已经创建了表,所以没有任何列可以保存这些信息。 如何在数据库上进行迁移以便反映此模型? 它有控制台工具吗? 我是否必须安装第三方gem?

Rails通过现有数据库进行迁移

我正在创建一个新的Rails 3.1应用程序。 我希望这个新的应用程序重用现有的数据库(由之前的rails 2应用程序创建)。 我创建了定义模型的新应用程序,它重用了数据库中的一些现有数据。 在开发和测试阶段,一切正常,因为它在一个干净的工作表数据库上运行,但在尝试部署到生产时,我得到的消息如下: PGError: ERROR: column “email” of relation “users” already exists *** [err :: localhost] : ALTER TABLE “users” ADD COLUMN “email” character varying(255) DEFAULT ” NOT NULL 但是我在我的迁移中认为是这样的 class DeviseCreateUsers false t.recoverable t.rememberable t.trackable t.timestamps end end 如何使db:migrate忽略已存在的内容并仅更改新内容和/或新类型? 我在stackoverflow上看到了类似的问题,但没有人回答这个问题。 谢谢你的回答。

什么是“循环参数引用”错误,使用activesupport time_zone?

我是ruby on rails的新手,我正在尝试创建一个教程。 我执行rake db:migrate时遇到问题。 hugo@ubuntu:~/pin_board$ rake db:migrate /home/hugo/.rvm/gems/ruby-2.2.2/gems/activesupport-4.0.5/lib/active_support/values/time_zone.rb:283: warning: circular argument reference – now 是什么导致这个? 有人能帮助我吗?

我应该在回滚后删除迁移吗?

我对ruby和铁轨都很陌生,而且我只是围绕着迁移。 我的问题是在回滚后删除迁移的最佳做法或时机是什么。 到目前为止,我所看到的是回滚后是否删除迁移是一个问题,但是在团队中工作时删除迁移是否有任何重大影响,离开迁移文件有什么好处而不是删除它? 在我的情况下最有意义的是什么? 我有我的原始迁移文件20140731141350_create_users.rb class CreateUsers < ActiveRecord::Migration def change create_table :users do |t| t.string :email t.string :password t.timestamps end end end 我需要添加一个salt列,所以我创建了迁移20140804125449_add_salt_colum_to_users.rb class AddSaltColumToUsers < ActiveRecord::Migration def change add_column :users, :salt, :string end end 但在开发过程中,我意识到盐柱不是必需的并且已经完成 rake db:migrate:down VERSION=20140731141350 现在我留下了一个未使用的20140804125449_add_salt_colum_to_users.rb迁移文件。 删除还是不?