在 Active Record 中添加自定义列数据类型

发布于 2024-10-17 01:51:28 字数 420 浏览 3 评论 0原文

在我的本地计算机上,我使用 MySQL 开发 Rails 应用程序,但在部署时我使用 Heroku,它使用 PostgreSQL。我需要创建一种新的数据类型,特别是我希望将其称为长文本,并且需要映射到任一数据库中的单独列类型。

我一直在寻找这个。我的基本想法是,我需要重写 ActiveRecord::ConnectionAdapters::*SQL 适配器内部的一些哈希,但我想我应该查阅这里的丰富知识,看看这是否是一个好方法(并且,如果可能的话,指导如何去做)或者是否有另一种快速获胜的方法。

现在数据类型是“字符串”,由于数据类型太长,我插入失败。我希望在 MySQL 和 PgSQL 上具有相同的功能,但似乎没有通用数据类型可以为我提供无限的文本 blob 列类型?

我的想法是,我希望这个应用程序能够针对两种数据库技术正常工作(通过迁移)。

非常感谢。

On my local machine I develop my Rails application using MySQL, but on deployment I am using Heroku which uses PostgreSQL. I am in need of creating a new data type, specifically I wish to call it longtext, and it is going to need to map to separate column types in either database.

I have been searching for this. My basic idea is that I am going to need to override some hash inside of the ActiveRecord::ConnectionAdapters::*SQL adapter(s) but I figured I would consult the wealth of knowledge here to see if this is a good approach (and, if possible, pointers on how to do it) or if there is a quick win another way.

Right now the data type is "string" and I am getting failed inserts because the data type is too long. I want the same functionality on both MySQL and PgSQL, but it looks like there is no common data type that gives me an unlimited text blob column type?

The idea is that I want to have this application working correctly (with migrations) for both database technologies.

Much appreciated.

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

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

发布评论

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

评论(2

忆沫 2024-10-24 01:51:28

为什么不在你的开发机器上安装 PostgreSQL?下载它,单击“确定”几次,然后就可以开始运行了。这不是火箭科学:-)

http://www.postgresql.org/download/

PostgreSQL对数据类型没有限制,您可以创建任何您想要的内容,这取决于您的想象力:

CREATE DOMAIN(仅简单内容)

创建类型(无限制)

Why don't you install PostgreSQL on your dev machine? Download it, click "ok" a few times and you're up and running. It isn't rocket science :-)

http://www.postgresql.org/download/

PostgreSQL doesn't have limitations on datatypes, you can create anything you want, it's up to your imagination:

CREATE DOMAIN (simple stuff only)

CREATE TYPE (unlimited)

余罪 2024-10-24 01:51:28

Frank 提到的 SQL 实际上就是答案,但我确实在寻找一种更具体的方法来执行 RDBMS 特定的 Rails 迁移。原因是我想保持我的应用程序可以在 PostgreSQL 和 MySQL 上运行的事实。

class AddLongtextToPostgresql < ActiveRecord::Migration
  def self.up
    case ActiveRecord::Base.connection.adapter_name
    when 'PostgreSQL'
      execute "CREATE DOMAIN longtext as text"
      execute "ALTER TABLE chapters ALTER COLUMN html TYPE longtext"
      execute "ALTER TABLE chapters ALTER COLUMN body TYPE longtext"
    else
      puts "This migration is not supported on this platform."
    end
  end

  def self.down
  end
end

这实际上就是我一直在寻找的东西。

The SQL that Frank mentioned is actually the answer, but I really was looking for a more specific way to do RDBMS specific Rails migrations. The reason is that I want to maintain the fact that my application can run on both PostgreSQL and MySQL.

class AddLongtextToPostgresql < ActiveRecord::Migration
  def self.up
    case ActiveRecord::Base.connection.adapter_name
    when 'PostgreSQL'
      execute "CREATE DOMAIN longtext as text"
      execute "ALTER TABLE chapters ALTER COLUMN html TYPE longtext"
      execute "ALTER TABLE chapters ALTER COLUMN body TYPE longtext"
    else
      puts "This migration is not supported on this platform."
    end
  end

  def self.down
  end
end

That is effectively what I was looking for.

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