mercredi 2 mars 2016

SQLite Java Multiple columns primary key not working

I am aware that there is an enormous amount of questions on this topic, but unfortunately nothing so far helped. I want a multi-colum primary key in my database. The db is on a server and should hold certificates for a number of users. The primary key for a certificate is serialnumber + issuer (which makes a certificate unique), additional to that - as multiple users could have this certificate - i want to add the (unique) username. My primary key therefore should be (serial, issuer, username). But it does not work. As soon as I try to add a certificate again but for a different user, he just overwrites the old entry.

My database setup looks as follows

stmt.execute(
                "CREATE TABLE IF NOT EXISTS certificates (" +
                        "serial VARCHAR NOT NULL," +     // serial
                        "issuer VARCHAR NOT NULL," +     // issuer
                        "subject VARCHAR NOT NULL," +    // subject
                        "publickey VARCHAR NOT NULL," +  // public key
                        "notbefore DATETIME NOT NULL," + // not before
                        "notafter DATETIME NOT NULL," +  // not after
                        "certdata BLOB," +               // DER-encoded certificate
                        "revoked BOOLEAN NOT NULL," +    // is certificate revoked
                        "trusted BOOLEAN NOT NULL," +    // is certificate trusted
                        "untrusted BOOLEAN NOT NULL," +  // is certificate untrusted
                        "S BOOLEAN NOT NULL," +          // is certificate in the S set
                        "" +                             //  of the related assessment
                        "username TEXT NOT NULL," + // the user to which this certificate belongs
                        "" +
                        "CHECK (S IN (0, 1))," +
                        "CHECK (trusted IN (0, 1))," +
                        "CHECK (untrusted IN (0, 1))," +
                        "CHECK (NOT (trusted = 1 AND untrusted = 1))," +
                        "" +
                        "FOREIGN KEY (username)" +
                        "  REFERENCES users(username)" +
                        "  ON DELETE CASCADE," +
                        "PRIMARY KEY (serial, issuer, username))");

The statement stmt comes from

poolManager = new MiniConnectionPoolManager(dataSource, MAX_CONNECTIONS);

    try (Connection connection = poolManager.getConnection();
         Statement stmt = connection.createStatement()) {

Instead of PRIMARY KEY (serial, issuer, username), I have already tried UNIQUE(serial, issuer, username) and CONSTRAINTS uniqueEntry PRIMARY KEY (serial, issuer, username). Both give the same result.

I am working with IntelliJ 15.0.3, the dialect is SQLite and the driver is sqlite-jdbc-2.8.11.2.jar .

Yes, there are some more tables to this, but they are of the same structure. If I find a solution for this table, all others should work accordingly.

Aucun commentaire:

Enregistrer un commentaire