I'm using H2 to test my database code. To do that, I start by reading in a set of SQL files and using RunScript.execute to execute the SQL.
My real database is MySQL, and I have tested the SQL scripts to make sure they work and they do. The problem is decidedly inside of H2. Here's the set of scripts:
CREATE TABLE stores (
id integer AUTO_INCREMENT PRIMARY KEY,
name varchar(255) NOT NULL,
UNIQUE(name),
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at timestamp
);
CREATE TABLE regions (
id integer AUTO_INCREMENT PRIMARY KEY,
name varchar(255) NOT NULL,
UNIQUE(name),
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at timestamp
);
CREATE TABLE products (
id integer AUTO_INCREMENT PRIMARY KEY,
store_id integer NOT NULL,
name varchar(255) NOT NULL,
region_id integer NOT NULL,
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at timestamp,
UNIQUE(store_id, name),
FOREIGN KEY store_fk(store_id) REFERENCES stores(id),
FOREIGN KEY region_fk(region_id) REFERENCES regions(id)
);
Running these 3 scripts to create the tables using MySQL Workbench works fine. Running them from JUnit via the follow code does not:
Reader storeTableSql = new InputStreamReader(
DatabaseConnectorTests.class.getResourceAsStream(
"/migrations/V1__create_stores_table.sql"));
Reader regionTableSql = new InputStreamReader(
DatabaseConnectorTests.class.getResourceAsStream(
"/migrations/V2__create_regions_table.sql"));
Reader productTableSql = new InputStreamReader(
DatabaseConnectorTests.class.getResourceAsStream(
"/migrations/V3__create_products_table.sql"));
this.dc = DatabaseConnector.getConnection();
RunScript.execute(dc, storeTableSql);
RunScript.execute(dc, regionTableSql);
RunScript.execute(dc, productTableSql);
I get the following error:
org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "CREATE TABLE PRODUCTS (
ID INTEGER AUTO_INCREMENT PRIMARY KEY,
STORE_ID INTEGER NOT NULL,
NAME VARCHAR(255) NOT NULL,
REGION_ID INTEGER NOT NULL,
CREATED_AT TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
UPDATED_AT TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
DELETED_AT TIMESTAMP,
UNIQUE(STORE_ID, NAME),
FOREIGN KEY STORE_FK[*](STORE_ID) REFERENCES STORES(ID),
FOREIGN KEY REGION_FK(REGION_ID) REFERENCES REGIONS(ID)
) "; expected "("; SQL statement:
CREATE TABLE products (
id integer AUTO_INCREMENT PRIMARY KEY,
store_id integer NOT NULL,
name varchar(255) NOT NULL,
region_id integer NOT NULL,
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at timestamp,
UNIQUE(store_id, name),
FOREIGN KEY store_fk(store_id) REFERENCES stores(id),
FOREIGN KEY region_fk(region_id) REFERENCES regions(id)
) [42001-196]
This code works in MySQL, and I am running H2 in MySQL compatibility mode via the following connection string: jdbc:h2:file:~/database;MODE=MySQL. I'm not sure what else I can do here. This error seems very strange to me, as I have no idea why it would think I need ANOTHER (.
Does anyone know how to fix this? Or have a better suggestion on how to seed my test database? I cannot use hibernate for this task due to a restriction on the system I am running on so I'm forced to do a lot manually.
Thank you!
From this post, but with a little more explanation.
Foreign key constraints in H2 do not allowed named constraints for whatever reason. The solution to this problem is to simply remove the name:
CREATE TABLE products (
id integer AUTO_INCREMENT PRIMARY KEY,
store_id integer NOT NULL,
name varchar(255) NOT NULL,
region_id integer NOT NULL,
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at timestamp,
UNIQUE(store_id, name),
FOREIGN KEY (store_id) REFERENCES stores(id),
FOREIGN KEY (region_id) REFERENCES regions(id)
);
The error message is so unclear and misleading. Hopefully this helps someone.
Related
I investigate one problem on the database (Aurora MySQL 5.7.mysql_aurora.2.10.0).
In my AWS service, we change the database structure automatically through the java code, namely through spring (schema.sql + spring.sql.init.mode=always). Examples: Create table if exist, alter table, etc.
And after some time, I get a DB "waiting for table metadata lock" error (photo below), which leads to the fall of the kubernetes pod (above limit CPU usage).
There is the same service working with GCP (Big Query) doing the same auto-configuration through the same piece of code and there is no such problem.
Schema.sql file
CREATE TABLE IF NOT EXISTS job_detail (
job_id varchar(50) NOT NULL,
created_by varchar(100) DEFAULT NULL,
dataflow_id varchar(50) DEFAULT NULL,
periodic_tasks longtext,
error_details longtext DEFAULT NULL,
end_date datetime DEFAULT NULL,
executed_date datetime DEFAULT NULL,
failed_records longtext,
total_records int(11) DEFAULT 0,
created_date datetime DEFAULT NULL,
updated_by varchar(100) DEFAULT NULL,
updated_date datetime DEFAULT NULL,
job_status varchar(100) DEFAULT NULL,
job_summary longtext,
job_name varchar(100) DEFAULT NULL,
project_id varchar(50) NOT NULL,
PRIMARY KEY (job_id)
);
ALTER TABLE job_detail CHANGE COLUMN created_by created_by VARCHAR(255) NULL DEFAULT NULL;
ALTER TABLE job_detail CHANGE COLUMN updated_by updated_by VARCHAR(255) NULL DEFAULT NULL;
Photo of queries logs
All answers to the issue "Incorrect string value" point to using utf8mb4. However, I'm still using that I still get that error.
Running
SELECT CCSA.character_set_name, collation_name FROM information_schema.`TABLES` T,
information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA
WHERE CCSA.collation_name = T.table_collation
AND T.table_schema = "test"
AND T.table_name = "products";
produces
character_set_name collation_name
utf8mb4 utf8mb4_unicode_ci
And running
SELECT character_set_name, collation_name FROM information_schema.`COLUMNS`
WHERE table_schema = "test"
AND table_name = "products"
AND column_name = "description";
produces the same result. Then, when I try to update the column description:
UPDATE test.products SET description='Esperienza molto negativa... 😁😁😁😁SCONSIGLIATISSIMO' WHERE id='50'
I get the abovementioned error
1366: Incorrect string value: '\xF0\x9F\x98\x81\xF0\x9F...' for column 'description' at row 1
I've tried this update both through a jdbc connection jdbc:mysql://localhost:3306/test?useUnicode=yes;characterEncoding=utf8; and directly through MySQLWorkbench. I don't know what else to try of look for. What am I missing?
EDIT
Output of SHOW CREATE TABLE
CREATE TABLE `products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`manufacturerId_fk` int(11) NOT NULL,
`type_fk` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'TEST',
`ownerId_fk` int(11) DEFAULT NULL,
`rating_fk` int(11) NOT NULL,
`description` varchar(2000) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`authorName` varchar(75) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`authorUrl` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `manufacturer_authorUrl_UNIQUE` (`manufacturerId_fk`,`authorUrl`),
KEY `fk_products_manufacturers_idx` (`manufacturerId_fk`),
KEY `fk_products_users_idx` (`ownerId_fk`),
KEY `fk_products_validRatings_idx` (`rating_fk`),
KEY `fk_products_productTypes` (`type_fk`),
CONSTRAINT `fk_products_productTypes` FOREIGN KEY (`type_fk`) REFERENCES `productTypes` (`type`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_products_manufacturers` FOREIGN KEY (`manufacturerId_fk`) REFERENCES `manufacturers` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_products_validRatings` FOREIGN KEY (`rating_fk`) REFERENCES `validRatings` (`value`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=418 DEFAULT CHARSET=utf8mb4
A similar question occurred to me with a text field.
(MySQL 1366 error incorrect string value)
I confirmed the database, table, column character set were all utf8mb4.
The problem was solved by changing the field type to longtext.
Hope this could help someone.
Caused by: java.sql.SQLException: Invalid default value for
'updated_at' Query:
CREATE TABLE IF NOT EXISTS saved_query (id int(11)
NOT NULL AUTO_INCREMENT,name varchar(255) NOT NULL,description
varchar(255) DEFAULT NULL,query longtext,params_json
longtext,created_at timestamp DEFAULT CURRENT_TIMESTAMP,updated_at
=timestamp NOT NULL, PRIMARY KEY (id)) Parameters: []
Pasting the query for readability:
CREATE TABLE IF NOT EXISTS saved_query
(
id INT(11) NOT NULL auto_increment,
name VARCHAR(255) NOT NULL,
description VARCHAR(255) DEFAULT NULL,
query LONGTEXT,
params_json LONGTEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL,
PRIMARY KEY (id)
)
Mysql version is 5.7.13. The query above works in versions 5.6 and 5.5. The following query works in 5.7 but not in <5.7 versions:
CREATE TABLE IF NOT EXISTS saved_query
(
id INT(11) NOT NULL auto_increment,
name VARCHAR(255) NOT NULL,
description VARCHAR(255) DEFAULT NULL,
query LONGTEXT,
params_json LONGTEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
)
The error in 5.6 is:
Caused by: java.sql.SQLException: Incorrect table definition; there
can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or
ON UPDATE clause Query:
CREATE TABLE IF NOT EXISTS saved_query (id
int(11) NOT NULL AUTO_INCREMENT,name varchar(255) NOT NULL,description
varchar(255) DEFAULT NULL,query longtext,params_json
longtext,created_at timestamp DEFAULT CURRENT_TIMESTAMP,updated_at
timestamp DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id)) Parameters: []
What should be the query that works in all these versions?
The problem is because of sql_modes. You have to remove the sql_mode to make it work. Remove these SQL modes
NO_ZERO_IN_DATE
NO_ZERO_DATE
or
before you run any query, paste this code in the first line of your code:
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
or
before you run any query, paste this code in the first line:
SET sql_mode = '';
TIMESTAMP can not be NULL, and mysql add DEFAULT CURRENT_TIMESTAMP for column. But may be only one column width DEFAULT CURRENT_TIMESTAMP. Just use '0000-00-00 00:00:00'.
CREATE TABLE IF NOT EXISTS saved_query
(
id INT(11) NOT NULL auto_increment,
name VARCHAR(255) NOT NULL,
description VARCHAR(255) DEFAULT NULL,
query LONGTEXT,
params_json LONGTEXT,
created_at TIMESTAMP DEFAULT '0000-00-00 00:00:00',
updated_at TIMESTAMP DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (id)
)
I'm using hibernate 4.3.7.Final on OSX with Mysql 5.5.8.
I have a joinTable relationship setup however my foreign keys fail to add:
[ERROR] org.hibernate.tool.hbm2ddl.SchemaExport - HHH000389: Unsuccessful: alter table T_USER_AUTHORITY add constraint FK_fr51fcyulxn31ijiotp4fx7i5 foreign key (email) references T_USER
[ERROR] org.hibernate.tool.hbm2ddl.SchemaExport - Can't create table 'carcloud.#sql-149c1_195' (errno: 150)
if I run SHOW ENGINE INNODB STATUS; I get the following:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
141106 5:48:00 Error in foreign key constraint of table carcloud/#sql-149c1_123:
foreign key (email) references T_USER:
Syntax error close to:
if i run:
alter table T_USER_AUTHORITY add constraint FK_fr51fcyulxn31ijiotp4fx7i5 foreign key (email) references T_USER (email);
The constraint is successfully added, note (email) at the end which is the referencing column.
It is set as my primary key:
CREATE TABLE `T_USER` (
`email` varchar(100) NOT NULL,
`created_by` varchar(50) NOT NULL,
`created_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`last_modified_by` varchar(50) DEFAULT NULL,
`last_modified_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`OPT_LOCK` int(11) DEFAULT NULL,
`first_name` varchar(50) DEFAULT NULL,
`last_name` varchar(50) DEFAULT NULL,
`password` varchar(100) DEFAULT NULL,
`phone` varchar(100) NOT NULL,
PRIMARY KEY (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Any ideas?
The only difference that i see is in Hibernate "references T_USER" and in sql "references T_USER (email)" so you should insert java code that call "alter table T_USER_AUTHORITY add constraint FK_fr51fcyulxn31ijiotp4fx7i5 etc .. "
I am working on a grails project. I have following query that I am trying to execute
String CHECK_FOR_HIGH_TRADE_VOLUME_QUERY = "Update LocationTrade lt set lt.hasVeryHighVolume=true where lt.locationIndices=? AND lt.trade.volume>20000";
...
LocationTrade.executeUpdate(CHECK_FOR_HIGH_TRADE_VOLUME_QUERY, [indices]);
The relationship between LocationTrade and Trade is unidirectional many-to-one. So, LocationTrade has a reference to Trade but Trade class does not have reference to the List of LocationTrade.
On execution, I get the following exception.
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute update query; SQL [update location_trade cross join set has_very_high_volume=1 where location_indices_id=? and volume>20000]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute update query
and
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'set has_very_high_volume=1 where location_indices_id=997 and volume>20000' at line 1
It seems that generated query is wrong. There should have been a join with the Trade table, but that is missing. I am unable to identify the error that I made here. Can some of you help me?
Creation Script for the two tables (I have stripped some of the uninteresting columns)
CREATE TABLE `location_trade` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`version` bigint(20) NOT NULL,
`auto_status` varchar(255) DEFAULT NULL,
`exclusion_reason_description` varchar(255) DEFAULT NULL,
`exclusion_reason_id` bigint(20) DEFAULT NULL,
`exclusion_reason_title` varchar(255) DEFAULT NULL,
`location_indices_id` bigint(20) DEFAULT NULL,
`manual_status` varchar(255) DEFAULT NULL,
`trade_id` bigint(20) DEFAULT NULL,
`absolute_price` decimal(19,6) DEFAULT NULL,
`flag` varchar(255) DEFAULT NULL,
`auto_exclusion_reason` varchar(255) DEFAULT NULL,
`date_created` datetime DEFAULT NULL,
`exclusion_reason_text` varchar(255) DEFAULT NULL,
`last_updated` datetime DEFAULT NULL,
`has_very_high_volume` bit(1) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK858985A90CAA966` (`location_indices_id`),
KEY `FK858985AB5FA6A69` (`trade_id`),
CONSTRAINT `FK858985A90CAA966` FOREIGN KEY (`location_indices_id`) REFERENCES `location_indices` (`id`),
CONSTRAINT `FK858985AB5FA6A69` FOREIGN KEY (`trade_id`) REFERENCES `trade` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=25405 DEFAULT CHARSET=latin1;
CREATE TABLE `trade` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`version` bigint(20) NOT NULL,
`comments` varchar(1020) DEFAULT NULL,
`end_date` datetime DEFAULT NULL,
`price` decimal(19,6) DEFAULT NULL,
`price_type` varchar(255) DEFAULT NULL,
`source_id` bigint(20) DEFAULT NULL,
`start_date` datetime DEFAULT NULL,
`trade_date` datetime DEFAULT NULL,
`trade_name` varchar(255) DEFAULT NULL,
`volume` decimal(19,6) DEFAULT NULL,
`volume_units` varchar(255) DEFAULT NULL,
`date_created` datetime DEFAULT NULL,
`last_updated` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK697F1642D085935` (`source_id`),
CONSTRAINT `FK697F1642D085935` FOREIGN KEY (`source_id`) REFERENCES `job_source` (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=26567 DEFAULT CHARSET=latin1;
Thanks
The Hibernate documentation says:
No join, either implicit or explicit, can be specified in a bulk HQL query. Sub-queries can be used in the where-clause, where the subqueries themselves may contain joins.
lt.trade.volume is an implicit inner join between LocationTrade and Trade, so the query is invalid. You'll have to rewrite it to something like the following:
update LocationTrade lt set lt.hasVeryHighVolume=true where lt.locationIndices=?
and lt.id in (
select lt2.id from LocationTrade lt2 where lt2.trade.volume > 20000)
Or you'll have to use a SQL query instead.