Heroku PostgreSQL 迁移问题

发布于 2024-11-01 13:22:34 字数 1355 浏览 0 评论 0原文

我正在研究基于 Rails 的网站的模型,并创建了一个名为 ItemAttributes 的模型类,它为特定项目定义了一组有效属性。例如,“床尺寸”是项目“床”的属性。我还创建了一个 AttributeValue 类,它定义了属性的有效值,例如“king”“queen”“full”等。

我试图通过迁移填充heroku上的PostgreSQL数据库,但它抛出了这个相当无用的错误(迁移在 SQLite 上本地工作):

PGError: ERROR:  current transaction is aborted, commands ignored until end of transaction block
:             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
              FROM pg_attribute a LEFT JOIN pg_attrdef d
                ON a.attrelid = d.adrelid AND a.attnum = d.adnum
             WHERE a.attrelid = '"attribute_values"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum

这是我正在使用的迁移:

class AddBedSizeValues < ActiveRecord::Migration
  def self.up
    id = ItemAttribute.find_by_name('bed size').id

    AttributeValue.create(
      :name => 'king',
      :item_attribute_id => id
    )
    AttributeValue.create(
      :name => 'queen',
      :item_attribute_id => id
    )
    AttributeValue.create(
      :name => 'full',
      :item_attribute_id => id
    )
    AttributeValue.create(
      :name => 'x-long twin',
      :item_attribute_id => id
    )
    AttributeValue.create(
      :name => 'twin',
      :item_attribute_id => id
    )
  end

I am working on the model of my rails based website, and created a model class called ItemAttributes, which defines a set of valid attributes for a specific item. For example, 'bed size' is an attribute of the item 'bed.' I have also created a AttributeValue class which defines valid values for Attributes, like 'king' 'queen' 'full' etc.

I am attempting to populate the PostgreSQL database on heroku with a migration, but it throws this rather unhelpful error (the migration works locally on SQLite):

PGError: ERROR:  current transaction is aborted, commands ignored until end of transaction block
:             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
              FROM pg_attribute a LEFT JOIN pg_attrdef d
                ON a.attrelid = d.adrelid AND a.attnum = d.adnum
             WHERE a.attrelid = '"attribute_values"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum

Here is the migration I am using:

class AddBedSizeValues < ActiveRecord::Migration
  def self.up
    id = ItemAttribute.find_by_name('bed size').id

    AttributeValue.create(
      :name => 'king',
      :item_attribute_id => id
    )
    AttributeValue.create(
      :name => 'queen',
      :item_attribute_id => id
    )
    AttributeValue.create(
      :name => 'full',
      :item_attribute_id => id
    )
    AttributeValue.create(
      :name => 'x-long twin',
      :item_attribute_id => id
    )
    AttributeValue.create(
      :name => 'twin',
      :item_attribute_id => id
    )
  end

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

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

发布评论

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

评论(1

沉鱼一梦 2024-11-08 13:22:34

当同一事务中存在较早的错误时,就会出现此错误。找到并解决该错误。如果您预计会出现某些错误并且不想回滚整个事务,则可以使用保存点。

编辑,一些说明

为了澄清,我看到错误的情况流程如下:

BEGIN; -- start of transaction
SOME QUERY; -- Causes error but error is ignored
SECOND QUERY; -- Causes the error you get because it is still in the same transaction

发生错误后,事务必须回滚并开始新的事务,然后才能继续,否则您必须回滚到保存点。

可能是您遇到了导致此错误的其他条件,但也许您应该检查 PGDATA/pg_log 中的日志,以检查您的引擎是否记录了某些被忽略的错误。

如果这没有帮助,您可以尝试创建迁移期间执行的所有实际查询(而不是 Rails 代码)的日志。

This error is given when in the same transaction there has been an earlier error. Find and resolve that error. You can use savepoints if you expect certain error and don't want to rollback the complete transaction.

Edit, some clarifications

To clarify, the cases where I have seen the error the flow was about as follows:

BEGIN; -- start of transaction
SOME QUERY; -- Causes error but error is ignored
SECOND QUERY; -- Causes the error you get because it is still in the same transaction

After an error the transaction must be rolledback and a new one started before you can continue, or you must rollback to a savepoint.

It could be you have hit some other condition that gives this error but maybe you should check the logs in PGDATA/pg_log to check if your engine is logging some error rails is ignoring.

If that doesn't help can you try to create a log of all the actual queries (instead of the rails code) executed during the migration.

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