sqlpostgresqlsyntax-errorcreate-table

Cannot generate random uuid syntax error at or near uuid_generate_v4


I want to generate a random uuid but I got the following error:

ERROR:  42601: syntax error at or near "uuid_generate_v4"
LINE 2:   id UUID uuid_generate_v4() PRIMARY KEY,

Here is my SQL code:

CREATE table cars (
  id UUID uuid_generate_v4() PRIMARY KEY,
  name varchar,
  cover varchar,
  price varchar,
  engine varchar,
  fuel varchar,
  firstRegistration varchar,
  ps smallint,
  km int,
  color varchar,
  environment text,
  equipment text,
  description text,
  created_at timestamp not null default current_timestamp
);


Solution

  • You're missing the default keyword:

    CREATE table cars (
      id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
      -- Here-^
      name varchar,
      cover varchar,
      price varchar,
      engine varchar,
      fuel varchar,
      firstRegistration varchar,
      ps smallint,
      km int,
      color varchar,
      environment text,
      equipment text,
      description text,
      created_at timestamp not null default current_timestamp
    );