I was trying to run following Query on my sql server :
CREATE TABLE `e_store`.`products`(
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(250) NOT NULL ,
`brand_id` INT UNSIGNED NOT NULL ,
`category_id` INT UNSIGNED NOT NULL ,
`attributes` JSON NOT NULL ,
PRIMARY KEY(`id`) ,
INDEX `CATEGORY_ID`(`category_id` ASC) ,
INDEX `BRAND_ID`(`brand_id` ASC) ,
CONSTRAINT `brand_id` FOREIGN KEY(`brand_id`) REFERENCES `e_store`.`brands`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE ,
CONSTRAINT `category_id` FOREIGN KEY(`category_id`) REFERENCES `e_store`.`categories`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE
);
I have already brands and categories tables on my e_store database.
But I got the following Error :
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'JSON NOT NULL ,
PRIMARY KEY(`id`) ,
INDEX `CATEGORY_ID`('category_id' ' at line 6
For those who are facing this issue similar to me:
MariaDB does not natively implement the JSON data type but it uses it as an alias for LONGTEXT
for compatibility reasons. According to the documentation (https://mariadb.com/kb/en/library/json-data-type/):
JSON is an alias for
LONGTEXT
introduced for compatibility reasons with MySQL'sJSON
data type. MariaDB implements this as aLONGTEXT
rather, as the JSON data type contradicts the SQL standard, and MariaDB's benchmarks indicate that performance is at least equivalent.In order to ensure that a a valid json document is inserted, the JSON_VALID function can be used as a CHECK constraint.
So if you are having issues with the JSON
data type in MariaDB, simply just change to LONGTEXT
. ;-)