Primary key constraints not defined, use FreeFormQuery to access table

Why is it possible with this kind of db table to use TableQuery:

private void initDatabase() {
try {
Connection conn = connectionPool.reserveConnection();
Statement statement = conn.createStatement();
try {
statement.executeQuery(“SELECT * FROM PERSONADDRESS”);
statement.executeQuery(“SELECT * FROM CITY”);
} catch (SQLException e) {
/*
* Failed, which means that the database is not yet initialized
* => Create the tables
*/
statement
.execute(“create table city (id integer generated always as identity, name varchar(64), version integer default 0 not null)”);
statement.execute(“alter table city add primary key (id)”);
statement
.execute("create table personaddress "
+ "(id integer generated always as identity, "
+ "firstname varchar(64), lastname varchar(64), "
+ "email varchar(64), phonenumber varchar(64), "
+ "streetaddress varchar(128), postalcode integer, "
+ "cityId integer not null, version integer default 0 not null , "
+ “FOREIGN KEY (cityId) REFERENCES city(id))”);
statement
.execute(“alter table personaddress add primary key (id)”);
}
statement.close();
conn.commit();
connectionPool.releaseConnection(conn);
} catch (SQLException e) {
e.printStackTrace();
}

But with this kind of it is not:

private void initDatabase() {
try {
Connection conn = connectionPool.reserveConnection();
Statement statement = conn.createStatement();
try {
statement.executeQuery(“SELECT * FROM DOMAINS”);
} catch (SQLException e) {
/*
* Failed, which means that the database is not yet initialized
* => Create the tables
*/
statement.execute(“CREATE TABLE domains (” +
“domain_id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,” +
“domain_name VARCHAR(255) NOT NULL,” +
“client_name VARCHAR(255) NOT NULL,” +
//“version integer default 0 not null” +
“CONSTRAINT uq_domain_name_client_name_domains UNIQUE (domain_name, client_name)” +
“)”);

            statement.execute("CREATE TABLE component_types (" +
                    "component_type_id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY," +
                    "component_type_name VARCHAR(255) NOT NULL," +
                    "component_prefix VARCHAR(255) NOT NULL" +
                    //"version integer default 0 not null" +
                    ")");

            statement.execute("CREATE TABLE components (" +
                    "component_id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY," +
                    "domain_id INTEGER," +
                    "component_name varchar(255) NOT NULL," +
                    "component_type_id INTEGER NOT NULL," +
                    //"version integer default 0 not null" +
                    "CONSTRAINT fk_domain_id_components FOREIGN KEY (domain_id) REFERENCES domains (domain_id)," +
                    "CONSTRAINT fk_component_type_id_components FOREIGN KEY (component_type_id) REFERENCES component_types (component_type_id)," +
                    ")");

            statement.execute("CREATE TABLE metrics (" +
                    "metrics_id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY," +
                    "component_id INTEGER," +
                    "metric_name varchar(255) NOT NULL," +
                    //"version integer default 0 not null" +
                    "CONSTRAINT fk_component_id_metrics FOREIGN KEY (component_id) REFERENCES components (component_id)," +
                    "CONSTRAINT uq_component_id_metrics_id_metrics UNIQUE (metrics_id, component_id)" +
                    ")");

            statement.execute("CREATE TABLE metric_values (" +
                    "metrics_id INTEGER PRIMARY KEY," +
                    "metric_value BIGINT NOT NULL," +
                    "metric_datetime DATETIME NOT NULL," +
                    //"version integer default 0 not null" +
                    "CONSTRAINT fk_metrics_id FOREIGN KEY (metrics_id) REFERENCES metrics (metrics_id)" +
                    ")");
        }
        statement.close();
        conn.commit();
        connectionPool.releaseConnection(conn);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

The error I get : com.vaadin.server.ServiceException: java.lang.IllegalArgumentException: Primary key constraints have not been defined for the table “DOMAINS”. Use FreeFormQuery to access this table

Any kind of help is appreciated.

The “version” fields are actually uncommented, but I am unable to edit the post…

Update:
I also tried this for every table:
statement.execute(“alter table [THE_TABLE_NAME]
add primary key ([THE_ID]
)”);
and removed the primary key definitions from the table definitions ofc.

Anyone ???

Hello Chris,

I am not able to reproduce the error you have described as I’m able to use the DOMAINS table with a TableQuery (using in-memory hsqldb 2.3.1), created using your example code.
Which database are you using? Can you verify that there is a primary key for that table before running the Vaadin application?

Olli