Framework: I can't change CamelCase column names by migration

Created on 3 Oct 2017  路  7Comments  路  Source: laravel/framework

  • Laravel Version: 5.4.*
  • PHP Version: 7.3
  • Doctrine/DBAL Version: 2.6

    Description:

For example I have this column named 'lowerCaseColumn' in my database.
I get this error when I run migration.

 [Doctrine\DBAL\Schema\SchemaException]
  There is no column with name 'lowercasecolumn' on table my_table'.

Steps To Reproduce:

Situation: You have columns named with CamelCase in your database.

You run migration

Schema::table('my_table', function (Blueprint $table) {
                $table->renameColumn('lowerCaseColumn', 'lower_case_column');

Most helpful comment

Hello,
I had the same problem using postgresql
The fix in my case :

Schema::table('my_table', function (Blueprint $table) {
    $table->renameColumn('"lowerCaseColumn"', 'lower_case_column');
});

A bit more explanations can be found in doctrine/dbal code :

            $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) .
                ' RENAME COLUMN ' . $oldColumnName->getQuotedName($this) . ' TO ' . $column->getQuotedName($this);

And here :

    /**
     * Gets the quoted representation of this asset but only if it was defined with one. Otherwise
     * return the plain unquoted value as inserted.
     */
    public function getQuotedName(AbstractPlatform $platform) : string

This means that if you provide the column name without the double quotes, the generated query will be something like ALTER TABLE table RENAME COLUMN lowerCaseColumn TO lower_case_column
In postgresql all identifiers that are not double quoted are transformed to lowercase

By providing the double quotes in camelCase column names you will get a query like ALTER TABLE table RENAME COLUMN "lowerCaseColumn" TO lower_case_column which will take into account the camelcase part during the migration

Hope it can be of help to someone,
Have a nice day

All 7 comments

Can't replicate, what's your database driver?

@themsaid I use mysql.

@hanachan1026 can't reproduce this using mysql.

@hanachan1026
in your migration you are doing:
lowerCaseColumn

In your error you are getting:
lowercasecolumn

That means the error is elsewhere. You might have named the column as lowercasecolumn but when you are renaming it, you are telling it to rename lowerCaseColumn and therefore it can't find it

@themsaid
@Dylan-DPC Sorry, this was about the different issue, I will now close this.

@hanachan1026 What was the issue?

Hello,
I had the same problem using postgresql
The fix in my case :

Schema::table('my_table', function (Blueprint $table) {
    $table->renameColumn('"lowerCaseColumn"', 'lower_case_column');
});

A bit more explanations can be found in doctrine/dbal code :

            $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) .
                ' RENAME COLUMN ' . $oldColumnName->getQuotedName($this) . ' TO ' . $column->getQuotedName($this);

And here :

    /**
     * Gets the quoted representation of this asset but only if it was defined with one. Otherwise
     * return the plain unquoted value as inserted.
     */
    public function getQuotedName(AbstractPlatform $platform) : string

This means that if you provide the column name without the double quotes, the generated query will be something like ALTER TABLE table RENAME COLUMN lowerCaseColumn TO lower_case_column
In postgresql all identifiers that are not double quoted are transformed to lowercase

By providing the double quotes in camelCase column names you will get a query like ALTER TABLE table RENAME COLUMN "lowerCaseColumn" TO lower_case_column which will take into account the camelcase part during the migration

Hope it can be of help to someone,
Have a nice day

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

lzp819739483 picture lzp819739483  路  3Comments

iivanov2 picture iivanov2  路  3Comments

Fuzzyma picture Fuzzyma  路  3Comments

klimentLambevski picture klimentLambevski  路  3Comments