Ich mache eine Masseneinfügung von doppelten Werten (Duplikat Typ und Uuid) mit der folgenden Postgres-Abfrage.Warum löst das Einfügen doppelter Werte diesen Fehler in Postgres auf einer Rails 5-Anwendung aus?
Ich sehe ein Fehler in meinen Logs wie ActiveRecord::StatementInvalid: PG::CardinalityViolation: ERROR: ON CONFLICT DO UPDATE command cannot affect row a second time HINT: Ensure that no rows proposed for insertion within the same command have duplicate constrained values.
Warum funktioniert diese Konfliktlösung nicht?
ActiveRecord::Base.connection.execute <<-POSTGRESQL
INSERT INTO #{Movie.table_name} (#{headers})
VALUES #{values}
ON CONFLICT (uuid, type) DO UPDATE SET video_id = EXCLUDED.video_id,
status = 1,
category = EXCLUDED.category, updated_at = EXCLUDED.updated_at
POSTGRESQL
CREATE TABLE movies (
id integer NOT NULL,
video_id integer NOT NULL,
category integer NOT NULL,
uuid character varying NOT NULL,
data json DEFAULT '{}'::json,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL,
type character varying DEFAULT 'FeatureLengthVideo'::character varying,
status integer DEFAULT 0 NOT NULL,
);
Danke, also, wenn der Primärschlüssel "ID" genannt wurde, konnte ich es beheben, indem ich 'id = EXCLUDED.id' hinzufügen? – Nona
Nein - Sie müssen die Daten korrigieren, so dass sie nicht zweimal dieselbe ID enthalten. –