Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

constraint naming conflict, for same tables with different prefix #15679

Closed
moritz-h opened this issue Sep 29, 2016 · 1 comment
Closed

constraint naming conflict, for same tables with different prefix #15679

moritz-h opened this issue Sep 29, 2016 · 1 comment

Comments

@moritz-h
Copy link
Contributor

  • Laravel Version: 5.3.4 (illuminate/database)
  • PHP Version: 7.0.8
  • Database Driver & Version: mysql 5.7.15

Description:

I create tables with a table prefix and set a foreign key constraint. (See reproduce example). Now i change the table prefix and want to create the same tables in the same database with a different prefix.
The second call ends with:
PHP Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table '#sql-[...]

I checked the generated queries in the mysql logfile and the error occurs on the last of this 6 queries.

create table `foo_groups` (`id` bigint unsigned not null auto_increment primary key, `name` varchar(255) not null) default character set utf8mb4 collate utf8mb4_unicode_ci
create table `foo_users` (`id` bigint unsigned not null auto_increment primary key, `group_id` bigint unsigned not null, `name` varchar(255) not null) default character set utf8mb4 collate utf8mb4_unicode_ci
alter table `foo_users` add constraint `users_group_id_foreign` foreign key (`group_id`) references `foo_groups` (`id`)

create table `bar_groups` (`id` bigint unsigned not null auto_increment primary key, `name` varchar(255) not null) default character set utf8mb4 collate utf8mb4_unicode_ci
create table `bar_users` (`id` bigint unsigned not null auto_increment primary key, `group_id` bigint unsigned not null, `name` varchar(255) not null) default character set utf8mb4 collate utf8mb4_unicode_ci
alter table `bar_users` add constraint `users_group_id_foreign` foreign key (`group_id`) references `bar_groups` (`id`)

The name of the constraint is the same as in the third query, so i get the error above. So perhaps the solution is to somehow add the table prefix to the constraint name, so this name becomes unique again.

Steps To Reproduce:

Minimal example with this 2 files. Just composer install and then php test.php.
Then change 'prefix' => 'foo_', to 'prefix' => 'bar_', and run again php test.php

composer.json:

{
    "require": {
        "illuminate/database": "5.x"
    }
}

test.php:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Capsule\Manager as Capsule;

require __DIR__.'/vendor/autoload.php';

$db_settings = [
    'driver' => 'mysql',
    'host' => '127.0.0.1',
    'port' => '3306',
    'database' => 'test',
    'username' => 'test',
    'password' => 'test',
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => 'foo_',
];

$capsule = new Capsule();
$capsule->addConnection($db_settings);
$capsule->bootEloquent();
$capsule->setAsGlobal();

$schema = $capsule->schema();

$schema->create('groups', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('name');
});

$schema->create('users', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->bigInteger('group_id')->unsigned();
    $table->string('name');
});
$schema->table('users', function (Blueprint $table) {
    $table->foreign('group_id')->references('id')->on('groups');
});
@themsaid
Copy link
Member

This is a duplicate of #7889

But thank you for reporting :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants