laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.51k stars 11.02k forks source link

Migrations error There is no column with name '{column_name}' on table '{table_name}'. #18551

Closed cesarcruzc closed 7 years ago

cesarcruzc commented 7 years ago

Description:

I have created a alter table migration for change the string length of a column and default value but when I run php artisan:migrate I got this error:

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

Steps To Reproduce:

  1. Schema empresa table:

php artisan make:migration create_empresa_table --create=table

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateEmpresaTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('empresa', function (Blueprint $table) {
            $table->increments('id');

            $table->string('nombre_comercial', 200);
            $table->string('nombre_legal', 200);
            $table->string('nit', 20);
            $table->string('telefono', 10);
            $table->string('direccion', 200);
            $table->string('correo', 100);

            $table->integer('tipo_sociedad_id')->unsigned();

            $table->integer('tipo_regimen_id')->unsigned();

            $table->string('codigo_ciuu', 40);

            $table->integer('tipo_contribuyente_id')->unsigned();

            $table->enum('responsable_iva', ['Si', 'No']);
            $table->enum('retencion_fuente', ['Si', 'No']);
            $table->enum('gran_contribuyente', ['Si', 'No']);
            $table->enum('auto_retenedor', ['Si', 'No']);
            $table->smallInteger('numero_empleados');
            $table->string('logo', 20);

            $table->integer('user_id')->unsigned();

            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('empresa');
    }
}
  1. Schema to alter table field

alter_edit_logo_default_value_on_empresa_table --table=empresa

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AlterEditLogoDefaultValueOnEmpresaTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('empresa', function (Blueprint $table) {
            $table->string('logo', 100)->default('public/empresa/logo/default.png')->change();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('empresa', function (Blueprint $table) {
            $table->string('logo', 20)->change();
        });
    }
}
cesarcruzc commented 7 years ago

I have to do this for the moment:

alter_edit_logo_default_value_on_empresa_table.php

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AlterEditLogoDefaultValueOnEmpresaTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {

        Schema::table('empresa', function (Blueprint $table) {

//            $table->string('logo', 100)->defaults('public/empresa/logo/default.png')->change();

            if (env('DB_CONNECTION') === 'mysql'){
                // Mysql
                DB::statement("ALTER TABLE empresa CHANGE COLUMN logo logo varchar(100) NOT NULL DEFAULT 'public/empresa/logo/default.png'");
            } else if (env('DB_CONNECTION') === 'pgsql'){
                // PostgreSQL
                DB::statement("ALTER TABLE empresa ALTER COLUMN logo TYPE varchar(100), ALTER COLUMN logo SET DEFAULT 'public/empresa/logo/default.png'");
            }
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('empresa', function (Blueprint $table) {

//            $table->string('logo', 20)->change();

            if (env('DB_CONNECTION') === 'mysql'){
                // Mysql
                DB::statement("ALTER TABLE empresa CHANGE COLUMN logo logo varchar(20)");

            } else if (env('DB_CONNECTION') === 'pgsql'){
                // PostgreSQL
                DB::statement("ALTER TABLE empresa ALTER COLUMN logo TYPE varchar(20)");
            }

        });
    }
}
deleugpn commented 7 years ago

If you're adding a new column, don't call the method change()

cesarcruzc commented 7 years ago

@deleugpn I am changing the length and the defaults value for the column logo on table empresa.

deleugpn commented 7 years ago

Did you install doctrine?

cesarcruzc commented 7 years ago

yes, I have installed doctrine/dbal composer require doctrine/dbal

srmklive commented 7 years ago

Try adding the create & table callbacks in the same file. I think this should work.

themsaid commented 7 years ago

Closing for lack of activity.

tjbourke commented 6 years ago

I have the exact same problem. I installed doctrine/dbal, I'm changing a string column length. When I use change() it says "no column with name" and when I leave out the change() call, it tries to add my column and says "Duplicate column name". Any help would be appreciated!

ereztdev commented 6 years ago

Same issue here, @themsaid why would this be closed if no one solved this issue?

gboor commented 5 years ago

Also ran into this issue. This is definitely still happening!

meinder-a commented 5 years ago

Hi, facing the same issue, any hint?

Phactory commented 5 years ago

i got this on new version of pgsql (on older it works normally) exception is wrong - this column exists

xyrintech commented 4 years ago

Yes, this issue exists

adnanhz commented 4 years ago

Me too with sqlite

frctnlss commented 4 years ago

I am experiencing this issue with https://github.com/optimistdigital/nova-notes-field migration when ran on our staging environment.

PHP 7.4.5 Laravel Framework 7.15.0 Laravel Nova 3.6.0 MySQL 5.7.26

I have also checked permissions as depicted in https://github.com/doctrine/dbal/issues/1990

Note: when ran locally with the same setup there were not any issues. difference is the operating system:

macelux commented 3 years ago

I am facing this issue. Any fixes yet?

MArK1done commented 3 years ago

I am also facing this issue. Any fix or solution aside from writing the SQL statement? Using Mysql 5.7, PHP 7.2, Laravel framework 6.18.38

ansien commented 2 years ago

Same issue still happening:

Works in my unit tests (sqlite), not on MySQL.

ansien commented 2 years ago

@themsaid This should probably be re-opened.

Sicklou commented 2 years ago

The issue still exists.

I am trying to change a default value of a column, but I got the same error "Column does not exists".

If I don't put the change method at the end, I got a "Duplicate column error".

Zsolt148 commented 2 years ago

Same here

phanan commented 2 years ago

For future adventurers: This error doesn't come from Laravel itself, but Doctrine DBAL. Upgrading it to a newer version (for me it's the latest to date - 3.x) may help.

alvinara23 commented 2 years ago

"php": "^7.3|^8.0", "doctrine/dbal": "^3.3",

still not working when i want to change not nullable to nullable column. the only way to change the column properties is the way @cesarcruzc mentions:

I have to do this for the moment:

alter_edit_logo_default_value_on_empresa_table.php

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AlterEditLogoDefaultValueOnEmpresaTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {

        Schema::table('empresa', function (Blueprint $table) {

//            $table->string('logo', 100)->defaults('public/empresa/logo/default.png')->change();

            if (env('DB_CONNECTION') === 'mysql'){
                // Mysql
                DB::statement("ALTER TABLE empresa CHANGE COLUMN logo logo varchar(100) NOT NULL DEFAULT 'public/empresa/logo/default.png'");
            } else if (env('DB_CONNECTION') === 'pgsql'){
                // PostgreSQL
                DB::statement("ALTER TABLE empresa ALTER COLUMN logo TYPE varchar(100), ALTER COLUMN logo SET DEFAULT 'public/empresa/logo/default.png'");
            }
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('empresa', function (Blueprint $table) {

//            $table->string('logo', 20)->change();

            if (env('DB_CONNECTION') === 'mysql'){
                // Mysql
                DB::statement("ALTER TABLE empresa CHANGE COLUMN logo logo varchar(20)");

            } else if (env('DB_CONNECTION') === 'pgsql'){
                // PostgreSQL
                DB::statement("ALTER TABLE empresa ALTER COLUMN logo TYPE varchar(20)");
            }

        });
    }
}
mehmetnky commented 2 years ago

Issue still exists. Creating a new column, carrying all the data from old column to the new one looks like the only solution rn. Does anyone have a better idea? (except @cesarcruzc's solution)