わかりました、それで、私には非常に奇妙な問題のように思えます。次の SQL で作成された Postgres テーブルがあります。
CREATE TABLE message
(
message text,
author integer,
thread integer,
id serial NOT NULL,
"createdAt" timestamp with time zone,
"updatedAt" timestamp with time zone,
CONSTRAINT message_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE message
OWNER TO glenselle;
メッセージモデルをデータベースに保存するために、Sails.js (Waterline ORM を使用) を使用しています。問題を特定しようとする過程で、新しいレコードを保存しようとするたびにテーブルを削除し始めましたが、動作は常に同じです。ORM は、作成者をユーザー モデルに関連付け、スレッドをスレッド モデルに関連付けるために、いくつかの関連付けを行っています。とにかく、レコードを保存しようとすると、最初に次のようになります。
ERROR: duplicate key value violates unique constraint "message_pkey"
DETAIL: Key (id)=(1) already exists.
STATEMENT: INSERT INTO "message" ("message", "author", "thread", "id", "createdAt", "updatedAt") values ($1, $2, $3, $4, $5, $6) RETURNING *
したがって、これは非常に理解しやすいはずです。テーブルには ID が 1 の行が既に存在するため、"message_pkey" 制約に違反しています。しかし皮肉なことに、データがないのです!だから私の質問はこれです.テーブルにデータがまったくない場合、Postgresが一意の制約違反をスローする原因となる可能性があるのは何ですか?
モデルを作成するために実行しているものは次のとおりです。
create: function(req, res) {
var self = this;
Thread.create({}, function(err, newThread) {
if(err) return console.log(err);
Message.create({message: req.body.conversation[0].message}, function(err, newMessage) {
if(err) return console.log(err);
// This returns an array of user ids
sails.controllers.thread.parseUserIds(req.body.participants, req.user, function(idList) {
// First, associate the message with the author
newMessage.author = req.user.id;
newMessage.save(function(err, savedMessage) {
if(err) return console.log(err);
// First, associate the participants with the thread
newThread.participants.add(idList);
// Next, associate the message with the thread
newThread.conversation.add(savedMessage);
newThread.save(function(err, savedThread) {
if(err) return console.log(err);
console.log('The thread looks to have been saved. Check it out!');
return res.json(savedThread);
});
});
});
});
});
},