Home > Back-end >  Laravel Table Migration (Table not being created)
Laravel Table Migration (Table not being created)

Time:11-17

I've ran in to a few little issues tonight surrounding migrations within my Laravel application.

I am trying to add a new custom field to my Accounts Controller. At the time of when I created a new migration, and ran php artisan migrate, I received confirmation that the migration was provisioned successfully.

Although, I couldn't see my new table created within my database.

I then created another migration, at which point I incorrectly typed in an incorrect class name within the migration file - When I attempt to run the command php artisan migrate:refresh, I receive the following error: Cannot declare class AccountAddOldid, because the name is already in use

I'm a little stuck with the issue surrounding creating a new table for a new custom field within my application along with being stuck with trying to resolve the Class Declaration issue. I'm not too sure how I can get rid of this error as I accidentally made a typo error within the class name.

Here's the code for my first migration (Where I attempted to create a new table for my custom field)

<?php

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

class AccountAppdboldid extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
       public function up()
    {
        Schema::table('accounts', function (Blueprint $table) {

            $table->string('appdb_oldid')->nullable();
        });
    }

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

            $table->dropColumn(['appdb_oldid']);

        });
    }
}

Error I am receiving when saving the page within my application where my new custom field is displaying:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'appdb_oldid' in 'field list' (SQL: update accounts set updated_at = 2021-11-16 21:51:48, appdb_oldid = 123 where id = 1018)

Any tips would be appreciated, as I don't want to lose any data within my application :-) Thank you!

CodePudding user response:

The problem is pretty clear:

Cannot declare class AccountAddOldid, because the name is already in use

You have two or more classes that has name AccountAddOldid, just change to another name like AccountAddOldidAgain or whatever else.

php artisan migrate only migrate which is not migrated, if you have same class name with migrated class you don't get the error. But php artisan migrate:refresh will reset all data and tables on your database, then do migration from beginning, so you will get that error if you have duplicate class name.

CodePudding user response:

did you check your database connection and confirmed that the connection you've set is the same database as you expect the new field is created? It is possible that your connection is set to another database, for example sqlite, and you are checking a MySQL database. (happened to me)

Try deleting the AccountAppdboldid and AccountAddOldid migrations and create a new one with the provided artisan command: php artisan make:migration add_appdb_oldid_to_accounts_table and add the $table->string('appdb_oldid')->nullable(); to the up() method

  • Related