Integrity constraint violation for Symfony User

209 Views Asked by At

I'm getting this following error

[Application] Jan 5 00:03:10 |CRITICA| CONSOL Error thrown while running command "make:migration". Message: "An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1052 Champ: 'TABLE_NAME' dans where clause est ambigu

When running the migration command after creating the user entity

created: src/Entity/User.php
created: src/Repository/UserRepository.php
updated: src/Entity/User.php
updated: config/packages/security.yaml

       

Success!

Next Steps:

  • Review your new App\Entity\User class.
  • Use make:entity to add more fields to your User entity and then run make:migration.
  • Create a way to authenticate! See https://symfony.com/doc/current/security.html PS C:\Users\chris\Desktop\projets\Matehub-Api> php bin/console make:migration

In ExceptionConverter.php line 72: An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1052 Champ: 'TABLE_NAME' dans where clause est ambigu
`

I don't really get what is happening considering that I just used the symfony command to generate the User class.

Ive been able to create the database without any issue using

php bin/console doctrine:database:create

THis is what I have in dev.log file

[2024-01-05T00:03:10.055039+00:00] doctrine.DEBUG: Executing statement: SELECT c.TABLE_NAME, c.COLUMN_NAME AS field, IF( c.COLUMN_TYPE = 'longtext' AND EXISTS( SELECT * from information_schema.CHECK_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = 'matehub' AND TABLE_NAME = c.TABLE_NAME AND CHECK_CLAUSE = CONCAT( 'json_valid(', c.COLUMN_NAME, ')' ) ), 'json', c.COLUMN_TYPE ) AS type, c.IS_NULLABLE AS null, c.COLUMN_KEY AS key, c.COLUMN_DEFAULT AS default, c.EXTRA, c.COLUMN_COMMENT AS comment, c.CHARACTER_SET_NAME AS characterset, c.COLLATION_NAME AS collation FROM information_schema.COLUMNS c INNER JOIN information_schema.TABLES t ON t.TABLE_NAME = c.TABLE_NAME WHERE c.TABLE_SCHEMA = ? AND t.TABLE_SCHEMA = ? AND t.TABLE_TYPE = 'BASE TABLE' ORDER BY ORDINAL_POSITION (parameters: array{"1":"matehub","2":"matehub"}, types: array{"1":2,"2":2}) {"sql":"SELECT c.TABLE_NAME, c.COLUMN_NAME AS field,\n IF(\n c.COLUMN_TYPE = 'longtext'\n AND EXISTS(\n SELECT * from information_schema.CHECK_CONSTRAINTS \n WHERE CONSTRAINT_SCHEMA = 'matehub'\n AND TABLE_NAME = c.TABLE_NAME\n AND CHECK_CLAUSE = CONCAT(\n 'json_valid(',\n c.COLUMN_NAME,\n ')'\n )\n ),\n 'json',\n c.COLUMN_TYPE\n ) AS type,\n c.IS_NULLABLE AS null,\n c.COLUMN_KEY AS key,\n c.COLUMN_DEFAULT AS default,\n c.EXTRA,\n c.COLUMN_COMMENT AS comment,\n c.CHARACTER_SET_NAME AS characterset,\n c.COLLATION_NAME AS collation\nFROM information_schema.COLUMNS c\n INNER JOIN information_schema.TABLES t\n ON t.TABLE_NAME = c.TABLE_NAME\n WHERE c.TABLE_SCHEMA = ? AND t.TABLE_SCHEMA = ? AND t.TABLE_TYPE = 'BASE TABLE' ORDER BY ORDINAL_POSITION","params":{"1":"matehub","2":"matehub"},"types":{"1":2,"2":2}} []

[2024-01-05T00:03:10.059570+00:00] console.CRITICAL: Error thrown while running command "make:migration". Message: "An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1052 Champ: 'TABLE_NAME' dans where clause est ambigu" {"exception":"[object] (Doctrine\DBAL\Exception\NonUniqueFieldNameException(code: 1052): An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1052 Champ: 'TABLE_NAME' dans where clause est ambigu at C:\Users\chris\Desktop\projets\MateHub-Api\vendor\doctrine\dbal\src\Driver\API\MySQL\ExceptionConverter.php:72)\n[previous exception] [object] (Doctrine\DBAL\Driver\PDO\Exception(code: 1052): SQLSTATE[23000]: Integrity constraint violation: 1052 Champ: 'TABLE_NAME' dans where clause est ambigu at C:\Users\chris\Desktop\projets\MateHub-Api\vendor\doctrine\dbal\src\Driver\PDO\Exception.php:28)\n[previous exception] [object] (PDOException(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1052 Champ: 'TABLE_NAME' dans where clause est ambigu at C:\Users\chris\Desktop\projets\MateHub-Api\vendor\doctrine\dbal\src\Driver\PDO\Statement.php:130)","command":"make:migration","message":"An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1052 Champ: 'TABLE_NAME' dans where clause est ambigu"} []

EDIT : Actually I think this might not be related to the User entity but to the installation itself as the error seems to remain even if I remove the User entity and repository

2

There are 2 best solutions below

2
paadamson On BEST ANSWER

Check your .env file for your DATABASE_URL and make sure the "serverVersion" is correct.

For instance, if connecting to MySQL 8, make sure it doesn't say "serverVersion=10.11.2-MariaDB", etc

2
Juan Morejón On

I got the same error. Symfony project test created Database test created Entity Test with a field test created symfony console make:migration

error(s)

otros % symfony console make:migration                              
    [critical] Error thrown while running command "make:migration". Message: "An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'TABLE_NAME' in where clause is ambiguous"
    
In ExceptionConverter.php line 72:                                                                                                      
    An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'TABLE_NAME' in where clause is ambiguous  
    
In Exception.php line 28:
    SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'TABLE_NAME' in where clause is ambiguous  
                                                                                                              
In Statement.php line 130:
    SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'TABLE_NAME' in where clause is ambiguous  
                                                                                                           
make:migration

composer.json => requirements section

"require": {
    "php": ">=8.1",
    "ext-ctype": "*",
    "ext-iconv": "*",
    "doctrine/doctrine-bundle": "^2.11",
    "doctrine/doctrine-migrations-bundle": "^3.3",
    "doctrine/orm": "^2.17",
    "symfony/console": "6.4.*",
    "symfony/dotenv": "6.4.*",
    "symfony/flex": "^2",
    "symfony/framework-bundle": "6.4.*",
    "symfony/runtime": "6.4.*",
    "symfony/yaml": "6.4.*"
},
"require-dev": {
    "symfony/maker-bundle": "^1.52"
},

Can anyone see the light at the end of the tunnel? Thanks in advance