1

Postgresql primary key not auto incrementing even when notation serial specified this is my table create script

CREATE TABLE public."Medication" (
    "Id" serial NOT NULL,
    "ResidentId" int4 NOT NULL,
    "PharmacyId" int4 NULL,
    "PhysicianId" int4 NULL,
    "ReleasedQty" float8 NOT NULL DEFAULT '0'::double precision,
    "PRNEffectiveTime" int4 NULL,
    "IsSTO" bool NOT NULL DEFAULT false,
    "IsPendingOrder" bool NOT NULL DEFAULT false,
    "IsPsychotropic" bool NOT NULL DEFAULT false,
    "IsINRRequired" bool NOT NULL DEFAULT false,
    "eSignature" varchar NULL,
    "eSignatureDate" timestamp NULL,
    "PhysicianId_X" int4 NULL,
    "IsWitnessSigReq" bool NULL,
    "IsInjection" bool NULL,
    "AdministerByRole" varchar NULL,
    "AdministerByUser" varchar NULL,
    "AlfId" int4 NOT NULL,
    CONSTRAINT "Medication_pkey" PRIMARY KEY ("Id", "AlfId")
)
PARTITION BY RANGE ("AlfId");

the value of Id always remains zero

5
  • 1
    Then you are apparently explicitly inserting 0 into that column. Commented Oct 21, 2020 at 9:54
  • 1
    Unrelated to your problem, but: you should really avoid those dreaded quoted identifiers. They are much more trouble than they are worth it. wiki.postgresql.org/wiki/… Commented Oct 21, 2020 at 9:54
  • I am using .net core to insert data and then it is happening Commented Oct 21, 2020 at 9:55
  • let me check if it contains value zero explicitly Commented Oct 21, 2020 at 9:55
  • @a_horse_with_no_name please post 0 value comment as answer Commented Oct 21, 2020 at 10:02

1 Answer 1

2

My query was inserting explicit value 0 and this was happening in .net core with Medication Object containing Id zero. when in my class I specified Id as Identity column then it fixed

Table("Medication")]
    public partial class Medication
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
    

I was not adding [DatabaseGenerated(DatabaseGeneratedOption.Identity)] and this is why it was happening

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.