Я создаю таблицы для обработки/выбранный вопрос/заданного ответ раздела вопроса безопасности нашей базы данных и получаю эту ошибку:PostgreSQL ограничения уникальности не уникально достаточно
нет никакого ограничения уникальности соответствующего данные ключей ссылаются таблицы «m_security_questions "
Не знаете, как я это исправим?
(Поскольку схема не будет строить б/с ошибкой, я не мог добавить SQL скрипки)
CREATE TABLE security_question --defines questions
(
id SERIAL PRIMARY KEY NOT NULL,
question character varying(1024) NOT NULL,
is_custom boolean DEFAULT FALSE NOT NULL
);
INSERT INTO security_question
(question,is_custom)
VALUES
('do you know the answer?',FALSE),
('Write your own question',TRUE);
CREATE TABLE m_security_questions
(--defines question a member chooses & allows free form question
-- id SERIAL NOT NULL,
-- I know adding id like this and making keeping same pk solves it
-- but isn't storing the extra sequence not needed?
member integer --REFERENCES member(m_no)
-- commented out reference for so only
NOT NULL,
question integer REFERENCES security_question(id) NOT NULL,
m_note text,
PRIMARY KEY(member,question)
);
-- here I add unique constraint but doesn't the primary already mean I have a unique index?
ALTER TABLE m_security_questions ADD CONSTRAINT m_security_questions_unique_member_question UNIQUE (member,question);
INSERT INTO m_security_questions
(member,question,m_note)
VALUES
(2,1,NULL),
(2,2,'How many marbles in this jar?');
CREATE TABLE m_security_answer --defines members given answer
(-- I want member & question here to line up w/ same from m_security_questions
member integer REFERENCES m_security_questions(member),
question integer REFERENCES m_security_questions(question) NOT NULL,
answer character varying(255) NOT NULL,
PRIMARY KEY (member,question)
);
-- here is where I get the error:
-- there is no unique constraint matching given keys for referenced table "m_security_questions"
INSERT INTO m_security_answer
(member,question,answer)
VALUES
(2,1,'yes'),
(2,2,'431');
Я думаю, что вы правы. 4-минутное ограничение на правильный ответ. лол. Вы сделаете это так или просто добавите новый id на m_security_questions (упомянутый в edit)? Что лучше? – gooddadmike
Ничего плохого в использовании двухколоночного внешнего ключа. Вероятно, вы захотите следить за тем, чтобы в столбце не было ограничения NOT NULL. NULLs и FOREIGN KEYS могут иногда удивлять. И AFAICT это НЕ NULL в одной из таблиц, а NULL разрешено в другом. –
Я видел это вскоре после того, как я разместил это. Оба должны быть NOT NULL. – gooddadmike