1

Rails を使用して、Postgres でスケジューリング アプリを作成しています。start_at と end_at の 2 つの timedate 列を持つモデル Shift があります。それを作成するための移行は次のとおりです。

class CreateShifts < ActiveRecord::Migration
  def change
    create_table :shifts do |t|
      t.references :user
      t.references :schedule
      t.datetime :start_at
      t.datetime :end_at
      t.string :position
      t.string :notes

      t.timestamps
    end
    add_index :shifts, :user_id
    add_index :shifts, :schedule_id
  end
end

レコードを作成しようとすると、次のエラー 500 出力が表示されます。

GError: ERROR:  column "end_at" is of type timestamp without time zone but expression is of type time without time zone at character 132
HINT:  You will need to rewrite or cast the expression.
: INSERT INTO "shifts" ("created_at", "end_at", "notes", "position", "schedule_id", "start_at", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"

DB を保存する前に有効な DateTime オブジェクトを作成しています。これは、ロガーを使用して表示したことが正しいためです。パラメータは次のようになります。

{"utf8"=>"✓",
 "authenticity_token"=>"qornHMTAdikZJP/sByPIg//fFuYHHAQH3M/0R9XnP+o=",
 "shift"=>{"user_id"=>"1",
 "start_at(1i)"=>"2011",
 "start_at(2i)"=>"11",
 "start_at(3i)"=>"14",
 "start_at(4i)"=>"00",
 "start_at(5i)"=>"01",
 "end_at(1i)"=>"2011",
 "end_at(2i)"=>"11",
 "end_at(3i)"=>"14",
 "end_at(4i)"=>"00",
 "end_at(5i)"=>"02",
 "position"=>"",
 "notes"=>""},
 "schedule_id"=>"1"}

ただし、両方のフィールドを Time.now に設定することで、コンソールからレコードを作成できます。

4

1 に答える 1

2

エラー メッセージは非常に明確です: Your $2in the INSERTstatement is of type timeinstead of type timestamp(datetime)

INSERT INTO "シフト"

("created_at", " end_at ", "notes", "position", "schedule_id", "start_at", "updated_at", "user_id")

値 ($1、$2、$3、$4、$5、$6、$7、$8) 返される "id"

鉱山を強調します。パラメータを混同している必要があります。$2明らかに、投稿の最後に表示するパラメータではありません。または、割り当てる前に誤って時間にキャストしてしまいました。割り当てた部分が$2質問に表示されないため、問題が発生する可能性が最も高くなります。

たぶん、あなたの質問のように、 ?timedateの代わりに言及するある種のタイプミスでしょう。datetime

于 2011-11-14T05:00:24.810 に答える