Ruby on Rails 的 varchar 迁移问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1663020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:54:45  来源:igfitidea点击:

varchar Migration question for Ruby on Rails

ruby-on-railsactiverecordmigrationrails-migrations

提问by bgadoci

I have created a new table including a column "note". The default is varchar(255) I believe but I wish to have this column be a text area vs. a field and to allow more data. I imagine that I would make this change in ActiveRecord::Migration file but I am curious as to the format. Do I simply change the varchar(255) to varchar(1000) for example? (if so what is the format?

我创建了一个包含“注释”列的新表。我相信默认值是 varchar(255) 但我希望此列是文本区域而不是字段并允许更多数据。我想我会在 ActiveRecord::Migration 文件中进行此更改,但我对格式很好奇。例如,我是否只需将 varchar(255) 更改为 varchar(1000) ?(如果是,格式是什么?

def self.up
    create_table :notes do |t|
      t.string :note :varchar(1000)
    end

Is that the right format? Furthermore, how do I get the entry field to be multiple rows. Sorry if this is easy stuff but I am new to programming and RoR. Thanks.

这是正确的格式吗?此外,如何让输入字段成为多行。对不起,如果这是简单的东西,但我是编程和 RoR 的新手。谢谢。

采纳答案by Siddhartha Reddy

You can simply use the 'text' type instead of 'string'.

您可以简单地使用“文本”类型而不是“字符串”。

def self.up
  create_table :notes do |t|
    t.text :note
  end
end

Using the 'text' type will result in database column of type TEXT. Varchar is usually limited to a maximum length of 255 (in MySQL, other RDBMSs have similar limits).

使用 'text' 类型将导致 TEXT 类型的数据库列。Varchar 通常限制为最大长度 255(在 MySQL 中,其他 RDBMS 也有类似的限制)。

If you use Rails' form helpers, a textareawill be output for this field (because it is of type 'text'). textareais the form element that accepts multi-line input.

如果您使用 Rails 的表单助手,则会为该字段输出一个文本区域(因为它是“文本”类型)。textarea是接受多行输入的表单元素。

Edit: If you've already migrated the create_table, you can create a new migration to change the column type:

编辑:如果您已经迁移了 create_table,您可以创建一个新的迁移来更改列类型:

def self.up
  change_column :notes, :note, :text
end

回答by The Who

The correct format would be

正确的格式是

t.string :note, :limit => 1000

make sure you are using a version of MySQL(or whichever database) which supports varchars longer than 256 characters.

确保您使用的是支持长度超过 256 个字符的 varchars 的 MySQL(或任何数据库)版本。

if you want to use a large text block it would be

如果你想使用大文本块,那就是

t.text :note

See http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.htmlfor more information

有关更多信息,请参阅http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html

回答by Ben Crouse

You can change the length with the limit option as so...

您可以使用限制选项更改长度,因此...

def self.up
  change_column :notes, :note, :string, :limit => 1000
end

回答by roberocity

Since I had a lot of data already stored I used

由于我已经存储了很多数据,因此我使用了

self.up
  change_column :notes, :note, :text, :limit => nil
end

If I left off the :limit => nil option then the column type would change from varchar to text, but it still had a max length of 255 characters.

如果我不使用 :limit => nil 选项,那么列类型将从 varchar 更改为 text,但它的最大长度仍为 255 个字符。