Home > Back-end >  SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; change
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; change

Time:08-25

goal: change the column meta-settings in table settings.

I'm trying to change a column name in a table I created before, so I followed the steps:

  1. composer require doctrine/dbal
  2. php artisan make:migration update_oldFileName_table
  3. update_oldNameFile_table.php
Schema::table('settings', function (Blueprint $table) {
    $table->renameColumn('meta-description', 'metaDescription');
});
  1. php artisan migrate

However, it shows me this error :

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 '-description metaDescription VARCHAR(255) NOT NULL' at line 1 (SQL: ALTER TABLE settings CHANGE meta-description metaDescription VARCHAR(255) NOT NULL)

I think the problem is because the - in "meta-description" but I want to change it!

CodePudding user response:

you can add backticks around the column name:

$table->renameColumn('`meta-description`', 'metaDescription');

the problem is that - is an operator. the backticks make sure that is ignored.

CodePudding user response:

You need doctrine/dbal package in order to perform this action.

composer require doctrine/dbal

Run your migration again, and the error should go away.

  • Related