如何插入带有 id(自动递增)PostgREST 的记录?

How to insert a record with id (auto increment) PostgREST?

我有一个功能

        axios.post('http://localhost:3000/unitsmeasure', {
            id: 20,
            name: 'name'
        })
        .then(function (response) {
            console.log(response);
        })
        .catch(function (error) {
            console.log(error);
        });

它在 table 中插入一个条目。它的作品。

但是当我不指定 id 时它不起作用。 id(序列主键)。

        axios.post('http://localhost:3000/unitsmeasure', {
            name: 'name'
        })
        .then(function (response) {
            console.log(response);
        })
        .catch(function (error) {
            console.log(error);
        });

这行不通

SQL Table:

CREATE TABLE "unitsmeasure" (
  "id" serial PRIMARY KEY,
  "name" varchar(100)
)

SQL转储:

CREATE TABLE "unitsmeasure" (
  "id" int8 NOT NULL DEFAULT nextval('"Request".unitsmeasure_id_seq'::regclass),
  "name" varchar(100) COLLATE "pg_catalog"."default"
);
ALTER TABLE "unitsmeasure" OWNER TO "postgres";

ALTER TABLE "unitsmeasure" ADD CONSTRAINT "unitsmeasure_pkey" PRIMARY KEY ("id");

最有可能的罪魁祸首是没有授予序列使用权限。

试试这个:

GRANT USAGE ON SEQUENCE unitsmeasure_id_seq TO user_name;

插入用户需要访问序列才能从序列中插入值。