Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

H2 Database Primary Key Violation

I am trying to make inserts on a table with a Primary key composed of 2 attributes. However when I try to make an insert and one of the attributes in the primary key is identical to one already inserted I get the following error:

Unique index or primary key violation: "CONSTRAINT_INDEX_CCC ON PUBLIC.ABWESENHEIT(DATUM) VALUES

Here is my table:

Create TABLE Abwesenheit (

    s_id INTEGER NOT NULL REFERENCES Schueler(id) ON DELETE CASCADE,
    entschuldigt BOOLEAN DEFAULT FALSE,
    datum TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    kommentar VARCHAR(40),
    kalendereintrag_id VARCHAR(40) REFERENCES Schoolsubject(name) ON DELETE CASCADE,
    deleted BOOLEAN DEFAULT FALSE,
    PRIMARY KEY (s_id,datum)

);

And here are 2 inserts which reproduce the problem:

insert into Abwesenheit (s_id, entschuldigt, kommentar,datum,kalendereintrag_id) values (1,'false','','2015-12-21 11:59:00.0','Geschichte');
insert into Abwesenheit (s_id, entschuldigt, kommentar,datum,kalendereintrag_id) values (2,'false','','2015-12-21 11:59:00.0','Geschichte');

Even though the "datum" attribute defaults to CURRENT_TIMESTAMP, I need to be able to insert custom timestamps (i.e. for retroactive entries) .

Given the fact that the "s_id" is different in the two inserts they should work. However they do not. Any ideas on what might be causing the problem?

Thanks in advance!

like image 823
Syn Avatar asked Dec 06 '25 05:12

Syn


1 Answers

The problem was caused by another table which was referencing my table, under the assumption that the dates in the table Abwesenheit were unique.

like image 129
Syn Avatar answered Dec 08 '25 18:12

Syn