2

I'm new in Loopback 3. And I need to define the model with the unique field. Email field should be unique. I'm using Postgresql as DB.

I have tried adding "unique": true option. Also, I have tried to follow these pieces of advice: Ensure unique field value in loopback model. But it didn't give the desired result.

 "properties": {
    "id": {
      "type": "number",
      "id": true,
      "generated": true,
      "postgresql": {
        "dataType": "bigint"
      }
    },
    "name": {
      "type": "string",
      "postgresql": {
        "dataType": "character varying"
      }
    },
    "email": {
      "type": "varchar",
      "postgresql": {
        "dataType": "character varying"
      }
    },
    "added_date": {
      "type": "date",
      "postgresql": {
        "dataType": "date"
      }
    }
  }

In the end result, I want to have a unique field in Postgres scheme. It should look like this in Postgres :

-- Table: public."user"

-- DROP TABLE public."user";

CREATE TABLE public."user"
(
    id bigint NOT NULL DEFAULT nextval('user_id_seq'::regclass),
    name character varying COLLATE pg_catalog."default",
    email character varying COLLATE pg_catalog."default",
    added_date date,
    CONSTRAINT user_pkey PRIMARY KEY (id),
    CONSTRAINT user_email_key UNIQUE (email)

)
WITH (
    OIDS = FALSE
)
TABLESPACE pg_default;

ALTER TABLE public."user"
    OWNER to postgres;

1 Answers1

0

I think these will work. Either add the index property to the column def, or add it to the indexes portion of the model. It's weird there isn't more documentation on this.

 "properties": {
    "id": {
      "type": "number",
      "id": true,
      "generated": true,
      "postgresql": {
        "dataType": "bigint"
      }
    },
    "name": {
      "type": "string",
      "postgresql": {
        "dataType": "character varying"
      }
    },
    "email": {
      "type": "varchar",
      "postgresql": {
        "dataType": "character varying"
      }
    },
    "added_date": {
      "type": "date",
      "postgresql": {
        "dataType": "date"
      }
    }
  },
  "indexes": {
    "EMAIL_INDEX": {
      "columns": "email",
      "kind": "unique"
    }
  }

Or alternatively

 "properties": {
    "id": {
      "type": "number",
      "id": true,
      "generated": true,
      "postgresql": {
        "dataType": "bigint"
      }
    },
    "name": {
      "type": "string",
      "postgresql": {
        "dataType": "character varying"
      }
    },
    "email": {
      "type": "varchar",
      "postgresql": {
        "dataType": "character varying"
      },
      "index": {"kind": "UNIQUE"}
    },
    "added_date": {
      "type": "date",
      "postgresql": {
        "dataType": "date"
      }
    }
  }
Marvin Irwin
  • 938
  • 7
  • 17
  • Thank for an answer, but it doesn't work in mine case. I should integrate my ORM model with PostgreSQL schemes that are using ```UNIQUE CONSTRAINT```, not ```UNIQUE INDEX```. As a solution, I have created ```UNIQUE CONSTRAINT``` after ```autoupdate``` or ```automugration```. – Sasha Malinovski May 06 '19 at 10:26