Skip to content
This repository has been archived by the owner on Dec 10, 2018. It is now read-only.

Migration ignoring table prefix #31

Closed
K4q4m3 opened this issue Jan 5, 2018 · 5 comments
Closed

Migration ignoring table prefix #31

K4q4m3 opened this issue Jan 5, 2018 · 5 comments
Assignees

Comments

@K4q4m3
Copy link

K4q4m3 commented Jan 5, 2018

After installing this package, and runing php artisan migration

 SQLSTATE[HY000]: General error: 1005 Can't create table 'boljacij_new.#sql-357d_1c82b7' (errno: 150) (SQL: alter table `promotion_pages` add constraint `promotion_pages_image_id_foreign` foreign key (`image_id`) references `images`
  (`id`) on delete restrict on update restrict)

config/database.php:

'connections' => [

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => 'bc_',
            'strict' => false,
            'engine' => 'InnoDB',
        ],

db migration file

Schema::create('promotion_pages', function (Blueprint $table) {
            $table->uuid('uuid');
            $table->string('title');
            $table->text('content');
            $table->integer('image_id')->unsigned()->default(1);
            $table->foreign('image_id')->references('id')->on('images')
                ->onUpdate('restrict')->onDelete('restrict');
            $table->primary('uuid');
        });

When I remove this package, everyting runs normal.

@brendt
Copy link
Contributor

brendt commented Jan 8, 2018

What points to an issue with table prefixes? It looks to me that the migrations fail to run because there's data in the tables you're trying to alter, which make a foreign key constraint fail.

What happens when you run the following on a test database php artisan migrate:fresh? Does it still fail?

@brendt brendt self-assigned this Jan 8, 2018
@K4q4m3
Copy link
Author

K4q4m3 commented Jan 8, 2018

Migration creates table without prefix. promotion_pages should be bc_promotion_pages

In SQLSTATE error you can see attempt of creating foreign key to table images, instead of bc_images.

@brendt
Copy link
Contributor

brendt commented Jan 8, 2018

How is the id field in the images blueprint built?

@K4q4m3
Copy link
Author

K4q4m3 commented Jan 8, 2018

Schema::create('images', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('path');
            $table->string('slug')->unique();
            $table->integer('author_id')->unsigned();
            $table->foreign('author_id')->references('id')->on('users')
                ->onUpdate('restrict')->onDelete('restrict');
            $table->timestamps();
            $table->softDeletes();
            
        });

@brendt
Copy link
Contributor

brendt commented Jan 16, 2018

This issue is fixed in 1.1.2.

@brendt brendt closed this as completed Jan 16, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants