'Postgres Unique violation: 7 ERROR: duplicate key value violates unique constraint "channels_pkey" DETAIL: Key (id)=(3) already exists

so i am getting this error whenever i try to insert a new record from this line of code:

Channel::insert(['customer_id' => $offer->order->user_id, 'merchant_id' => $offer->user_id, 'offer_id' => $offer->id]);

and this is my channels table:

    CREATE SEQUENCE IF NOT EXISTS channels_id_seq

-- Table Definition
CREATE TABLE "public"."channels" (
    "id" int8 NOT NULL DEFAULT nextval('channels_id_seq'::regclass),
    "offer_id" int8 NOT NULL,
    "customer_id" int8 NOT NULL,
    "merchant_id" int8 NOT NULL,
    "created_at" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY ("id")
);

the sql query looks like this:

(SQL: insert into "channels" ("customer_id", "merchant_id", "offer_id") values (1, 3, 616))

i tired changing the order of variables and other things but it just doesnt seem to work



Solution 1:[1]

Please see this post on resetting sequences in PostgreSQL

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vesa Karjalainen