简体   繁体   中英

MySQL / MariaDB not accepting JSON Format? Can not create Database

I am currently using XAMPP/Apache with MariaDB on phpmyadmin. I am trying to create a table based on my code using Doctrine and therefore Annotations for validating a form. I simply want to store the entered values from the form in the database. In another example this worked perfectly fine.

But now i am having a "Checkbox field" that i guess is causing some kind of trouble when creating the database.

I am using those commands in the console:

php bin/console make:migration

Afterwards:

php bin/console doctrine:migrations:migrate

When calling the 2nd one = when i try to create the table in my database i get the following errors:

Migration 20181121103017 failed during Execution. 
Error An exception occurred while executing 'CREATE TABLE pizza (id INT 
AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, address VARCHAR(255) NOT NULL
phone VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL, 
size INT NOT NULL, ingredient JSON NOT NULL COMMENT '(DC2Type:json_array)', delivery INT NOT NULL, 
PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB':


SQLSTATE[42000]: Syntax error or access violation: 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 COMMENT '(DC2Type:json_
array)', delivery INT NOT NULL, PRIMARY KEY' at line 1

I am really not knowing what i am doing wrong at this point.

These are my annotations i have in my "Pizza.php" File.

// ------------------

/**
 * @Assert\NotBlank(
 *     message = "E-Mail Address required"
 * )
 * @Assert\Email(
 *     message = "The email '{{ value }}' is not a valid email."
 * )
 * @Assert\Length(min="2", max="255")
 * @ORM\Column(type="string", length=255)
 */

protected $email;

// ------------------

/**
 * @Assert\Range(min=0, max=3)
 * @ORM\Column(type="integer")
 */

protected $size;

// ------------------

/**
 *@Assert\NotBlank(
 *     message = "Choose at least 1 ingredient!"
 * )
 * @ORM\Column(type="array")
 */

protected $ingredient;

// ------------------

/**
 * @Assert\NotBlank(
 *     message = "Choose a delivery option!"
 * )
 * @Assert\Range(min=0, max=1)
 * @ORM\Column(type="integer")
 */

protected $delivery;

The form itself is working perfectly fine, the validations do work the way i intended.

What exactly am i doing wrong?

If you need any more code from my "Pizza.php" (Entity Class) or my Controller File where i did my routing please let me know.

I am grateful for any help!

Based on your comments, it seems that Doctrine thinks it can use features that are not available on your version of mariadb.

If you tell doctrine which version you are using, it will select the correct datatype for that column, in this case probably LONGTEXT or something similar.

Depending on what you are using, it would look something like (using a yaml file in symfony for example):

doctrine:
    dbal:
        server_version: '10.1'

Note that you would need to re-generate your migrations.

Like I mentioned in my comment, personally I would normalize the database and use a different table to link the pizza's to the ingredients to make searching and filtering easier.

First check your mariadb version. Version 10.1 doesn't support JSON datatype and support for version 10.2 is incomplete.

A workaround is to the version in doctrine.yaml file to

server_version: '5.6'

then regenerate the getters and setters with

php bin/console make:entity --regenerate

then generate the migration file with

php bin/console make:migration

this will generate a migration file with datatype set to LONGTEXT .

Afterwards, in the src/Migrations open each file in there and check for any migration file with JSON as datatype and delete all migration with such datatype. Remember, if you don't delete these files, the next command iterates with each file persisting them to database one by one starting with the old one. If such file exist it will trigger the error again.

Lastly, run

php bin/console doctrine:migrations:migrate

which will persist all the migrations to database accordingly.

Had the same issue and i found this workaround:

In Doctrine.yaml found out that the server_version was higher than my MariaDB version, so I corrected that in this line:

   doctrine:
        dbal:
            server_version: '5.5'

I did not work until I manually erased the migrations i already did. You can found them in src/Migrations .

Then run again:

 php bin/console make:migration
 php bin/console doctrine:migrations:migrate

And that did the work, hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM