Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
GitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
Cannot insert into table with DEFAULT value #695
Imagine that there is a table called 'resource' with only one auto_increment column called 'resourceId'.
The following code snippet runs well on H2 but fails on Postgres:
The generated SQL is (works with H2, tested):
In PostgreSQL this does not work. The following would work (tested):
DEFAULT should work on SQLServer, MySQL, CUBRID, and Oracle as well (not tested).
In MySQL NULL also works (not tested):
In SQLite columns with default values should not appear in values (question is how it behaves with one column table).
I could imagine to have a DefaultExpressionImpl like ConstantExpressionImpl that could be translated with SQLTemplates to the necessary value. With many of the database engines this could do the trick. The problem could come when a DB engine needs the default columns to be left out from values.
As much as I see the "DEFAULT VALUES" keyword is supported with many database engines as well. This could be a function of SQLInsertClause.
Thinking loudly, I will test the followings tomorrow:
There could be an defaultValues() function in SQLTemplates that would be called under the hood if no values are provided in the insert clause. The function would provide the following:
I will test them tomorrow. Not all of them but I can test Postgres, H2, HSQLDB, Oracle, SQLServer, SQLite and Derby. The other should be ok based on google-ing :)
As much as I see the SQLSerializer class should be changed a little bit in the method serializeForInsert(). In case the list of values is empty, the SQLTemplates instance should be called.
Please check these two commits:
I am sorry but I have no more time to test with databases. Based on google, every other databases should be ok (that are not tested).
Shall I write unit tests? If yes, how? I can imagine writing tests into the *TemplatesTest classes.
The only database where I could not find a perfect solution is Derby. It does not support any of the syntax. I made a solution that works if there is only one column in the table but it fails if there are more columns. In that case at least one column should be added into the insert into statement with the default keyword.
If everything is ok, I will send the pull request.
These commits look.
You can add a test case here https://github.com/mysema/querydsl/blob/master/querydsl-sql/src/test/java/com/mysema/query/InsertBase.java
The test suites are here https://github.com/mysema/querydsl/tree/master/querydsl-sql/src/test/java/com/mysema/query/suites
I wrote a simple test that inserts into the table survey. The tests fail on two databases:
Derby: I expected that as there are more columns in the survey table.
Based on HSQLDB doc it should work: http://hsqldb.org/web/features200.html See "New Data Manipulation Language Features" chapter. Is it possible that the primary key of the table Surve does not have a default value in HSQLDB? It has in Postgres or MySQL.
How shall we proceed? Shall we switch the test off in case of these two databases?
In case of Derby I can imagine only a solution where the Default type is implemented like ConstantImpl so the use can define the value "DEFAULT" by hand when he/she creates an InsertClause.