简体   繁体   中英

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; change column

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!

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.

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.

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