Ruby on Rails 的 varchar 迁移问题

发布于 2024-08-09 05:56:48 字数 354 浏览 3 评论 0原文

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

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

这是正确的格式吗?此外,如何使输入字段成为多行。抱歉,如果这是简单的事情,但我是编程和 RoR 的新手。谢谢。

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?

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.

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

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

发布评论

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

评论(4

卖梦商人 2024-08-16 05:56:48

正确的格式将

t.string :note, :limit => 1000

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

如果您想使用大文本块,请

t.text :note

参阅 http://api。 rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html 了解更多信息

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.

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

t.text :note

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

╰沐子 2024-08-16 05:56:48

您可以使用 limit 选项更改长度,如下所示......

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

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

def self.up
  change_column :notes, :note, :string, :limit => 1000
end
晚雾 2024-08-16 05:56:48

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

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

使用“文本”类型将产生 TEXT 类型的数据库列。 Varchar 通常限制最大长度为 255(在 MySQL 中,其他 RDBMS 也有类似的限制)。

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

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

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

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).

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

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

def self.up
  change_column :notes, :note, :text
end
凉城凉梦凉人心 2024-08-16 05:56:48

由于我已经存储了很多数据,所以我使用了

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

If I left off the :limit =>; nil 选项,则列类型将从 varchar 更改为 text,但最大长度仍然为 255 个字符。

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.

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