相关疑难解决方法(0)

当推送到Heroku时,Rails迁移错误w/Postgres

我正在尝试执行以下向上迁移以更改"tweet"模型表中的"number"列

class ChangeDataTypeForTweetsNumber < ActiveRecord::Migration
  def up
    change_column :tweets do |t|
      t.change :number, :integer
    end
  end

  def down
    change_table :tweets do |t|
      t.change :number, :string
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

执行以下向上迁移到heroku ....

heroku rake db:migrate:up VERSION=20120925211232
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

    PG::Error: ERROR:  column "number" cannot be cast to type integer
: ALTER TABLE "tweets" ALTER COLUMN "number" TYPE integer
Run Code Online (Sandbox Code Playgroud)

任何想法都会非常感激.

感谢大家.

postgresql ruby-on-rails heroku rails-migrations

21
推荐指数
2
解决办法
7411
查看次数

Postgresql运算符类"varchar_pattern_ops"不接受数据类型整数

我正在尝试将我的数据id postgresql从字符串迁移到django中的整数,以便在sphinx搜索中使用它们.首先,我正在进行数据迁移,将数据转换为字符串整数

db.execute('''UPDATE the_table SET foo='1' WHERE foo='bar';''')
Run Code Online (Sandbox Code Playgroud)

然后我正在进行架构迁移

ALTER TABLE the_table ALTER COLUMN col_name TYPE integer USING (col_name::integer);
Run Code Online (Sandbox Code Playgroud)

就像在这里被告知的那样

但是我收到了一个错误

错误:运算符类"varchar_pattern_ops"不接受数据类型整数

SQL-состояние:42804

South和pgAdmin都会发生此错误.数据是正确的 - 字符串类型为Null或整数.我究竟做错了什么?

sql django postgresql

8
推荐指数
2
解决办法
4563
查看次数

如何使用 Rails 在 PostgreSQL 中将时间列更改为整数列?

我在连接到新 Rails 应用程序的 PostgreSQL 数据库中命名duration的表中有一个列time_entries。它目前被格式化为时间数据,但我希望它是一个整数。(具体来说,我要使用 smallint 列,因为它将是不超过一天的分钟数,即 1440。)

首先,我试过:

change_column :time_entries, :duration, :smallint, limit: 2
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:

PG::DatatypeMismatch: ERROR:  column "duration" cannot be cast automatically to type smallint
HINT:  You might need to specify "USING duration::smallint".
Run Code Online (Sandbox Code Playgroud)

然后,在看了这篇文章这篇文章之后,我尝试了以下迁移:

change_column :time_entries, :duration, 'integer USING CAST(duration AS integer)'
change_column :time_entries, :duration, :smallint, limit: 2
Run Code Online (Sandbox Code Playgroud)

但是第一行返回了以下错误:

PG::CannotCoerce: ERROR:  cannot cast type time without time zone to integer
Run Code Online (Sandbox Code Playgroud)

我如何让它转换?时区无关紧要,因为它实际上表示一段时间。我是一个 Rails 新手,对原始 SQL 代码一无所知。谢谢!

postgresql casting ruby-on-rails rails-migrations ruby-on-rails-4

5
推荐指数
1
解决办法
1159
查看次数

将JSON列类型迁移到HSTORE列类型

我目前有以下数据库模式:

create_table :user_actions do |t|
  t.integer  :emitter_id
  t.string   :emitter_type
  t.integer  :target_id
  t.string   :target_type
  t.json     :payload
  t.timestamps
end
Run Code Online (Sandbox Code Playgroud)

而且我想将这个payload领域迁移jsonhstore.

执行以下操作:

change_column :user_actions, :payload, :hstore
Run Code Online (Sandbox Code Playgroud)

导致以下错误消息:

PG::DatatypeMismatch: ERROR:  column "payload" cannot be cast automatically to type hstore
HINT:  Specify a USING expression to perform the conversion.
Run Code Online (Sandbox Code Playgroud)

不知道如何使用USING提示以及在不丢失任何数据的情况下执行此迁移的最佳方法是什么?

postgresql activerecord hstore ruby-on-rails-4

4
推荐指数
1
解决办法
3954
查看次数