Rails 4:SQLException:没有这样的表: [英] Rails 4: SQLException: no such table:

查看:37
本文介绍了Rails 4:SQLException:没有这样的表:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Rails4 中运行以下内容:

i am running the following in Rails4:

$ bundle exec rake db:migrate

$ bundle exec rake db:migrate

== 201405270646 AddAttachmentImageToPins:迁移 ============================-- change_table(:pins) 抽水中止!标准错误:发生错误,这次和所有以后的迁移都取消了:

== 201405270646 AddAttachmentImageToPins: migrating =========================== -- change_table(:pins) rake aborted! StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: 没有这样的表:pins: ALTER TABLE "pins" ADDimage_file_name"varchar(255)c:/Sites/pinteresting/db/migrate/201405270646_add_attachment_image_to_pins.rb:4:in block in up'c:/Sites/pinteresting/db/migrate/201405270646_add_attachment_image_to_pins.rb:3:inup' c:in `migrate' 任务:TOP => db:migrate(查看完整跟踪使用 --trace 运行任务)

SQLite3::SQLException: no such table: 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: inup' c:in `migrate' Tasks: TOP => db:migrate (See full trace by running task with --trace)

我无法理解为什么我会收到此错误.

I can't undestand why i am getting this error.

这是我的 github:https://github.com/frankzk/pinteresting

Here is my github: https://github.com/frankzk/pinteresting

感谢您的帮助

迁移文件:

class AddAttachmentImageToPins < ActiveRecord::Migration
  def self.up
    change_table :pins do |t|
        t.attachment :image
    end
  end


  def self.down
    drop_attached_file :pins, :image
  end
end

推荐答案

它与迁移的文件名有关.

It has to do with the filename of the migration.

运行迁移时,Rails 会查看文件名中的时间戳,以确定运行它们的顺序.

When running migrations, Rails looks at the timestamps in the filenames to figure out the order to run them in.

如果您查看迁移文件:

db/migrate/
├── 20140526033741_devise_create_users.rb
├── 20140526222538_create_pins.rb
├── 20140527032853_add_user_id_to_pins.rb
└── 201405270646_add_attachment_image_to_pins.rb

您会看到 add_attachment_image_to_pins.rb 由于某种原因比其他字符短 2 个字符.所以它试图首先运行这个,在那个时候,pins 表还没有创建,导致 no such table: pins 错误

You'll see that add_attachment_image_to_pins.rb is 2 characters shorter than the others for some reason. So it's trying to run this one first and at that point, the pins table hasn't been created leading to the no such table: pins error

将其重命名为 20140527064600_add_attachment_image_to_pins.rb 使我能够成功运行迁移.

Renaming it to 20140527064600_add_attachment_image_to_pins.rb allowed me to run the migrations successfully.

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

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