Heroku db:推送错误(rails)

发布于 2024-12-11 08:50:56 字数 2444 浏览 0 评论 0原文

当我运行 $heroku db:push 时,出现以下错误:

Saving session to push_201110231302.dat..
!!! Caught Server Exception
HTTP CODE: 500
Taps Server Error: PGError: ERROR:  value too long for type character varying(255)

此处的讨论< /a> 表明这是因为我的一个模型的字符串属性超过 255 个字符。

我想我在“课程”模型的“描述”属性中发现了问题。

我尝试通过将描述的类型从字符串更改为文本来解决此问题。我通过生成并运行“remove_descrip_from_Course descrip:string”类型迁移,然后生成/运行“add_descrip_to_Course descrip:text”来完成此操作。但这不起作用——descrip 仍然显示为字符串。

有谁知道(1)将描述从字符串更改为文本是否是解决heroku问题的​​最佳(唯一?)方法,以及(2)我可以做什么来将描述从字符串更改为文本?

谢谢!

编辑:

这是我的 schema.rb 文件的相关部分(描述位于底部):

  create_table "courses", :force => true do |t|
    t.string   "name"
    t.string   "number"
    t.string   "instructor"
    t.string   "room"
    t.string   "day"
    t.string   "units"
    t.string   "time"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "limitations"
    t.string   "address"
    t.string   "end_time"
    t.string   "start_time"
    t.string   "crn"
    t.string   "term_code"
    t.text     "evals"
    t.boolean  "paper_required"
    t.string   "exam_type"
    t.string   "paper_type"
    t.string   "past_instructors"
    t.string   "past_semesters"
    t.string   "tod"
    t.boolean  "in_cart",          :default => false
    t.integer  "day_num"
    t.integer  "time_num"
    t.string   "units_alt"
    t.text     "descrip"
  end

这是 add_descrip_to_course 迁移:

class AddDescripToCourse < ActiveRecord::Migration
  def self.up
    add_column :courses, :descrip, :text
  end

  def self.down
    remove_column :courses, :descrip
  end
end

但这就是我的 Rails 控制台中发生的情况:

ruby-1.9.2-p290 :008 > a = Course.new
 => #<Course id: nil, name: nil, number: nil, instructor: nil, room: nil, day: nil, units: nil, time: nil, created_at: nil, updated_at: nil, limitations: nil, address: nil, end_time: nil, start_time: nil, crn: nil, term_code: nil, evals: nil, paper_required: nil, exam_type: nil, paper_type: nil, past_instructors: nil, past_semesters: nil, tod: nil, in_cart: false, day_num: nil, time_num: nil, units_alt: nil, descrip: nil> 
ruby-1.9.2-p290 :009 > a.descrip = "test"
 => "test" 
ruby-1.9.2-p290 :010 > a.descrip.class
 => String 

When I run $heroku db:push, I get the following error:

Saving session to push_201110231302.dat..
!!! Caught Server Exception
HTTP CODE: 500
Taps Server Error: PGError: ERROR:  value too long for type character varying(255)

The discussion here suggests that this is because one of my models has a string attribute that's longer than 255 chars.

I think I identified the problem in the "descrip" attribute of my "Course" model.

I tried fixing this by changing descrip's type from string to text. I did this by generating and running a "remove_descrip_from_Course descrip:string" type migration, then generating/running an "add_descrip_to_Course descrip:text." But that didn't work -- descrip still shows up as a string.

Does anyone know if (1) changing descrip from a string to text is the best (only?) way of solving the heroku problem, and (2) what I can do to change descrip from string to text?

Thanks!

EDITED:

Here's the relevant portion of my schema.rb file (descrip is at bottom):

  create_table "courses", :force => true do |t|
    t.string   "name"
    t.string   "number"
    t.string   "instructor"
    t.string   "room"
    t.string   "day"
    t.string   "units"
    t.string   "time"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "limitations"
    t.string   "address"
    t.string   "end_time"
    t.string   "start_time"
    t.string   "crn"
    t.string   "term_code"
    t.text     "evals"
    t.boolean  "paper_required"
    t.string   "exam_type"
    t.string   "paper_type"
    t.string   "past_instructors"
    t.string   "past_semesters"
    t.string   "tod"
    t.boolean  "in_cart",          :default => false
    t.integer  "day_num"
    t.integer  "time_num"
    t.string   "units_alt"
    t.text     "descrip"
  end

And here's the add_descrip_to_course migration:

class AddDescripToCourse < ActiveRecord::Migration
  def self.up
    add_column :courses, :descrip, :text
  end

  def self.down
    remove_column :courses, :descrip
  end
end

But this is what happens in my rails console:

ruby-1.9.2-p290 :008 > a = Course.new
 => #<Course id: nil, name: nil, number: nil, instructor: nil, room: nil, day: nil, units: nil, time: nil, created_at: nil, updated_at: nil, limitations: nil, address: nil, end_time: nil, start_time: nil, crn: nil, term_code: nil, evals: nil, paper_required: nil, exam_type: nil, paper_type: nil, past_instructors: nil, past_semesters: nil, tod: nil, in_cart: false, day_num: nil, time_num: nil, units_alt: nil, descrip: nil> 
ruby-1.9.2-p290 :009 > a.descrip = "test"
 => "test" 
ruby-1.9.2-p290 :010 > a.descrip.class
 => String 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦开始←不甜 2024-12-18 08:50:56

第一步是在同一个堆栈上进行开发和部署;在 MySQL 或 SQLite 之上开发,然后在 PostgreSQL 之上部署只是自找麻烦,PostgreSQL(幸运的是)比 SQLite 和 MySQL 严格得多。因此,如果您要部署到 Heroku,请在本地安装 PostgreSQL。

要更改列类型,您应该能够在迁移中使用它:

def self.up # or "def change" or "def up" depending on your Rails version
    change_column :courses, :descrip, :text
end

这应该在 PostgreSQL 和 TEXT“可变无限长度”字符类型。

为此使用 :text 是获得任意大文本列的最佳方法。

如果您真的不想要无限的长度,那么您应该在模型中包含长度验证。 MySQL 会默默地截断你的数据,而 SQLite 会忽略字符串列的长度,而 PostgreSQL 则不会做这些事情。

在 Rails/ActiveRecord 世界中,所有字符串类型都是 String 的实例,因此 char(n)varchar(n)text 都可以作为字符串输出并作为字符串输入。

Your first step is to develop and deploy on the same stack; developing on top of MySQL or SQLite and then deploying on top of PostgreSQL is just asking for trouble, PostgreSQL is (thankfully) a lot stricter than SQLite and MySQL. So install PostgreSQL locally if you're going to be deploying to Heroku.

To change the column type, you should be able to use this in a migration:

def self.up # or "def change" or "def up" depending on your Rails version
    change_column :courses, :descrip, :text
end

That should give you a column of type TEXT in PostgreSQL and TEXT is a "variable unlimited length" character type.

Using :text for this is the best way to get an arbitrarily large text column.

If you don't really want an unlimited length then you should include length validations in your model. MySQL will silently truncate your data and SQLite ignores length on string columns, PostgreSQL does neither of these things.

In the Rails/ActiveRecord world, all stringish types are instances of String so char(n), varchar(n), and text all come out as String and go in as String.

歌枕肩 2024-12-18 08:50:56

我曾经遇到过类似的问题(我必须将字符串更改为文本),并遇到了相同的障碍。我最终不得不进行迁移,删除整个表并使用正确的列类型重新创建。

I had a similar problem at one time (I had to change a string to a text), and ran into the same obstacles. I eventually had to make a migration that dropped the entire table and recreated again with the proper column types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文