Ruby on Rails DB 迁移脚本使用文本而不是 :string varchar

发布于 2024-08-12 17:27:49 字数 373 浏览 2 评论 0原文

我是 Ruby on Rails 的初学者,所以如果这很明显,我深表歉意,但我正在尝试学习如何编写数据库迁移脚本,并且我想将以下 long_description 更改为文本值而不是字符串:

class CreateArticles < ActiveRecord::Migration
  def self.up  
    create_table :articles do |t|
      t.column "short_description", :string
      t.column "long_description", :string
      t.timestamps
    end
  end
end

有什么想法这怎么可能吗?

I'm a beginner at Ruby on Rails so I apologize if this is quite obvious, but I'm trying to learn how to write the database migration scripts and I'd like to change the following long_description to a text value instead of string:

class CreateArticles < ActiveRecord::Migration
  def self.up  
    create_table :articles do |t|
      t.column "short_description", :string
      t.column "long_description", :string
      t.timestamps
    end
  end
end

Any ideas how this is possible?

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

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

发布评论

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

评论(3

情深缘浅 2024-08-19 17:27:49
class CreateArticles < ActiveRecord::Migration
  def self.up
    create_table :articles do |t|
      t.string :short_description
      t.text :long_description
      t.timestamps
    end
  end
  def self.down
    # don't forget the down method
  end
end

另外,不要忘记 down 方法。

此处列出了迁移类型。

  • :字符串
  • :文本
  • :整数
  • :浮点数
  • :小数
  • :日期时间
  • :时间戳
  • :时间
  • :日期
  • :二进制
  • :布尔值
class CreateArticles < ActiveRecord::Migration
  def self.up
    create_table :articles do |t|
      t.string :short_description
      t.text :long_description
      t.timestamps
    end
  end
  def self.down
    # don't forget the down method
  end
end

Also, don't forget the down method.

Migration types are listed here.

  • :string
  • :text
  • :integer
  • :float
  • :decimal
  • :datetime
  • :timestamp
  • :time
  • :date
  • :binary
  • :boolean
友谊不毕业 2024-08-19 17:27:49
create_table :articles do |t|
  t.column 'long_description', :text
  # ...
end
create_table :articles do |t|
  t.column 'long_description', :text
  # ...
end
江城子 2024-08-19 17:27:49

将其设置为 :text

这是一个很好的参考: 这里

Set it to :text

Here's a good ref for you: Here

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