sari
sari

Reputation: 27

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!

Upvotes: 2

Views: 1243

Answers (2)

Gert B.
Gert B.

Reputation: 2362

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.

Upvotes: 2

Sumit Wadhwa
Sumit Wadhwa

Reputation: 3217

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.

Upvotes: 5

Related Questions