Tag: rails migrations

ActiveModel :: MissingAttributeError:无法写入未知属性`user_id`

我正在尝试实施“评论”function,作为我正在建设的项目的一部分。 在本课程的早些时候,我们创建了一个评论表,并让Faker gem生成虚假评论。 我的说明如下: 注释必须与用户关联,因此将user_id外键添加到comments表中。 记得也要添加一个索引; 更新用户模型,以便您可以调用user.comments和Comment模型,以便您可以调用comment.user; 运行db:reset时,修改seeds.rb文件以创建有效注释; 最初我试图运行我的rails generate命令,但一直遇到这个错误: ▶ rake db:migrate == 20150508143445 CreateComments: migrating =================================== — create_table(:comments) rake aborted! StandardError: An error has occurred, this and all later migrations canceled: SQLite3::SQLException: table “comments” already exists: CREATE TABLE “comments” (“id” INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, “description” text, “created_at” datetime NOT NULL, “updated_at” […]

Rake db:放弃我的旧桌子

我的用户迁移过去看起来像这样: class CreateUsers < ActiveRecord::Migration def change create_table :users do |t| t.string :login etc 现在它看起来像这样: class CreateUsers < ActiveRecord::Migration def change create_table :users do |t| t.string :username etc 那么,为什么我会看到这个? rake db:drop rake db:create rake db:migrate rails console > User.new +—-+——-+——————+—————+——————-+————+————+ | id | login | crypted_password | password_salt | persistence_token | created_at | updated_at | […]

从命令行添加has_many和belongs_to migration

我生成了两个模型,现在想要实现活动记录关联。 我有设计师和物品。 Item属于Designer,Designer有许多Items。 我的模型看起来像这样: 应用程序/模型/ item.rb的: class Item < ActiveRecord::Base belongs_to :designer validates :designer_id, presence: true end 应用程序/模型/ designer.rb: class Designer < ActiveRecord::Base has_many :items, dependent: :destroy end 即使在我运行rake db:migrate我的迁移也没有反映出新的关系。 他们展示了原始一代: class CreateDesigners < ActiveRecord::Migration def change create_table :designers do |t| t.string :name t.string :country t.string :about t.timestamps end end end class CreateItems < ActiveRecord::Migration […]

如何将数据从生产数据库迁移到开发数据库(Rails 4)?

我知道这听起来有些倒退! 我一直在研究基于云的案例管理应用程序,并且正在开发支持票务function。 我们的开发数据库(MySQL)拥有与生产数据库相同的所有数据(这是一个非常大的应用程序)。 开发基本上是一个“沙箱”环境,因此开发数据库与生产数据库完全相同。 今天早上,我在本地开发服务器中遇到了一个问题: Migrations are pending; run ‘bin/rake db:migrate RAILS_ENV=development’ to resolve this issue. 好吧,那样做,即使它没有任何意义。 它错了,因为它试图创建已经存在的表(我已经运行了我的迁移,function在一天前完成了!一切都很好)。 在我的开发服务器中收到此消息之前,我唯一能做的就是取消注释一个完全注释掉的文件来尝试修复TinyMCE(/config/tinymce.yml,文本编辑器)的问题,然后重启我的rails开发服务器。 所有这个文件都是一堆默认和插件。 当我看到这个问题时,我首先将它们全部评论回来。 还是一样的错误。 继续rake db:tasks。 开始寻找答案..不断出错。 犯了一个noob错误,最终做了一个 rake db:migrate:reset 我知道会以管理db的方式重建模式。 这失败了..但是我们确实在不止一个地方有数据库(因为它和生产一样)。另外,只是为了尝试将所有表格带回来: rake db:schema:load ….无论如何,在其中一张桌子上失败了 ActiveRecord::StatementInvalid: Mysql2::Error: Specified key was too long; max key length is 767 bytes: CREATE UNIQUE INDEX `login` USING btree ON `users` (`login`, […]

迁移失败:PG :: ProtocolViolation Rails

我刚刚更新到rails4并意识到我的db角色和数据库不知何故没有设置。 所以我删除了数据库并重新创建了app_development数据库和角色。 现在我正在尝试运行rake db:migrate并且它失败并出现以下错误: == DeviseCreateUsers: migrating ============================================== — create_table(:users) -> 0.0081s — add_index(:users, :email, {:unique=>true}) -> 0.0031s — add_index(:users, :reset_password_token, {:unique=>true}) -> 0.0030s == DeviseCreateUsers: migrated (0.0144s) ===================================== rake aborted! An error has occurred, this and all later migrations canceled: PG::ProtocolViolation: ERROR: bind message supplies 1 parameters, but prepared statement “a1” requires 0 : […]

在rails迁移中设置不同的默认值?

我正在尝试编写迁移,它看起来像这样: class AddStatusToWorks < ActiveRecord::Migration def self.up change_table :works do |t| t.string :status end end def self.down change_table :works do |t| t.remove :status end end end 事实是,我想根据已经在表中的布尔值“完成”为“状态”设置不同的默认值。 如果complete = true,则status =“complete”。 如果没有,status =“正在进行中”。 (之所以我想要一个字符串而不是保持完整的布尔值是因为我希望能够为状态提供两个以上的可能性。)任何想法如何做到这一点? 我只是像这样坚持if语句 change_table :works do |t| t.string :status if (:complete == true) :value => “complete” else :value => “wip” end 呃,所以看起来不太对劲。 我google了一下,发现你可以设置:默认值,但这不是我想要的。 […]

Rails 4:SQLException:没有这样的表:

我在Rails4中运行以下代码: $ bundle exec rake db:migrate == 201405270646 AddAttachmentImageToPins:迁移=========================== – change_table(:pins)rake aborted! StandardError:发生错误,此以及所有后续迁移都已取消: SQLite3 :: SQLException:没有这样的表:pins:ALTER TABLE“pins”ADD“image_file_n ame”varchar(255)c:/ Sites / pinteresting / db / migrate / 201405270646_add_attachment_im age_to_pins.rb:4:in block in up’ c:/Sites/pinteresting/db/migrate/201405270646_add_attachment_image_to_pins.rb:3: in up’c block in up’ c:/Sites/pinteresting/db/migrate/201405270646_add_attachment_image_to_pins.rb:3: in ‘c: block in up’ c:/Sites/pinteresting/db/migrate/201405270646_add_attachment_image_to_pins.rb:3: in migrate’任务:TOP => db:migrate(通过使用–trace运行任务来查看完整跟踪) 我无法理解为什么我会收到此错误。 这是我的github: https : //github.com/frankzk/pinteresting 谢谢您的帮助 迁移文件: […]

Rails + Postgres迁移 – 为什么我收到错误“PG :: UndefinedFunction:ERROR:function gen_random_uuid()不存在”?

我的一个Rails迁移使用uuid作为主键。 Postgres扩展gen_random_uuid()应解决此问题,但在安装相关扩展( uuid-ossp )后我仍然会收到错误。

Rails 3迁移:boolean(mysql vs postgreSQL)

我正在尝试在论坛主题上添加“粘性”选项。 这就是我的迁移的样子 def self.up add_column :topics, :sticky, :boolean, :null => false, :default => false end def self.down remove_column :topics, :sticky end 这在mysql上本地完美,但是当我将更改推送到heroku(使用PostgreSQL)时,这就是我在使用控制台时所获得的 >> t.sticky => “f” >> t.sticky.class => String >> t.sticky = true => true >> t.sticky.class => TrueClass 为什么这个属性的默认值是String? 编辑:如果我保存对象,它不会改变粘性属性,即它仍然是“f”。

轨道中的外键4

我正在使用Rails 4和SQLite。 我正在尝试在indicators表中添加外键。 请参阅下面的代码 class Indicator < ActiveRecord::Base belongs_to :objetive belongs_to :responsible, class_name: "Person" end 迁移脚本: class AddFksToIndicator < ActiveRecord::Migration def change add_reference :indicators, :objective, index: true add_reference :indicators, :responsible, index: true end end 当运行迁移时一切正常,所以我尝试在控制台中: 2.0.0p247 :002 > i = Indicator.new => # 2.0.0p247 :002 > i.objective_id = 0 2.0.0p247 :003 > i.save 令我惊讶的是,指标已经保存,并且没有id = […]