skip to Main Content

Really struggling to create a table with simple sql on my postgres database cant understand the error im getting. pic attached.

CREATE TABLE IF NOT EXISTS public.charters (
    id integer NOT NULL DEFAULT nextval('charters_id_seq'::regclass),
    length numeric,
    beam numeric,
    height numeric,
    passengers integer,
    berths integer,
    description text,
    "year" integer,
    power integer,
    price numeric,
    model character varying,
    skipper boolean,
    fuel_included boolean,
    gps boolean,
    shower boolean,
    main_img character varying,
    title character varying,
    created_at timestamp(6) without time zone NOT NULL,
    updated_at timestamp(6) without time zone NOT NULL,
    "location" character varying,
    PRIMARY KEY(id)
);

enter image description here

2

Answers


  1. Did you create a sequence charters_id_seq? Looks like the database can’t find it. Using an IDENTITY might be easier, you don’t have to create the sequence yourself:

    CREATE TABLE IF NOT EXISTS public.charters (
        id integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
        length numeric,
        beam numeric,
        height numeric,
        passengers integer,
        berths integer,
        description text,
        "year" integer,
        power integer,
        price numeric,
        model character varying,
        skipper boolean,
        fuel_included boolean,
        gps boolean,
        shower boolean,
        main_img character varying,
        title character varying,
        created_at timestamp(6) without time zone NOT NULL,
        updated_at timestamp(6) without time zone NOT NULL,
        "location" character varying
    );
    
    Login or Signup to reply.
  2. You will have to create a sequence charters_id_seq :

    Exemple :

    CREATE SEQUENCE charters_id_seq START 101;
    

    Then :

    CREATE TABLE IF NOT EXISTS public.charters (
        id integer NOT NULL DEFAULT nextval('charters_id_seq'::regclass),
        length numeric,
        beam numeric,
        height numeric,
        passengers integer,
        berths integer,
        description text,
        "year" integer,
        power integer,
        price numeric,
        model character varying,
        skipper boolean,
        fuel_included boolean,
        gps boolean,
        shower boolean,
        main_img character varying,
        title character varying,
        created_at timestamp(6) without time zone NOT NULL,
        updated_at timestamp(6) without time zone NOT NULL,
        "location" character varying,
        PRIMARY KEY(id)
    );
    

    To associate the sequence to the specific table column, such that if that column (or its whole table) is dropped, the sequence will be automatically dropped as well, then use this :

    ALTER SEQUENCE charters_id_seq OWNED BY public.charters.id ;
    

    This will avoid permissions issues as well

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search