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

Can not create a unique index on a column of an existing table #3

Open
ngmy opened this issue Aug 31, 2016 · 0 comments
Open

Can not create a unique index on a column of an existing table #3

ngmy opened this issue Aug 31, 2016 · 0 comments

Comments

@ngmy
Copy link

ngmy commented Aug 31, 2016

Package Version
laravel/framework 5.1.43 (LTS)
rafis/schema-extended 1.0.1
MySQL 5.5.46

I can not create a unique index on a column of an existing table.

I have two migrations files.

The first one is the 2016_08_29_135919_create_users_table.php file:

<?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('email');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }
}

The second is the 2016_08_29_140747_add_index_to_users_table_email_column.php file:

<?php

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

class AddIndexToUsersTableEmailColumn extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->unique('email');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropUnique('users_email_unique');
        });
    }
}

When I using SchemaExtended\Schema and executing the Artisan migrate command, the following error occur:

$ php artisan migrate


  [Illuminate\Database\QueryException]                                                                                                                                  
  SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the  
   right syntax to use near ')' at line 1 (SQL: alter table `users` add unique users_email_unique())                                                                    



  [PDOException]                                                                                                                                                        
  SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the  
   right syntax to use near ')' at line 1                                                                                                                               


The second migration seem is not applied.

mysql> desc users;
+------------+------------------+------+-----+---------------------+----------------+
| Field      | Type             | Null | Key | Default             | Extra          |
+------------+------------------+------+-----+---------------------+----------------+
| id         | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| email      | varchar(255)     | NO   |     | NULL                |                |
| created_at | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| updated_at | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
+------------+------------------+------+-----+---------------------+----------------+
4 rows in set (0.00 sec)

When I using Illuminate\Support\Facades\Schema and executing the Artisan migrate command, the error does not occur:

$ php artisan migrate
Migrated: 2016_08_29_135919_create_users_table
Migrated: 2016_08_29_140747_add_index_to_users_table_email_column

Two of the migration has been applied:

mysql> desc users;
+------------+------------------+------+-----+---------------------+----------------+
| Field      | Type             | Null | Key | Default             | Extra          |
+------------+------------------+------+-----+---------------------+----------------+
| id         | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| email      | varchar(255)     | NO   | UNI | NULL                |                |
| created_at | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| updated_at | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
+------------+------------------+------+-----+---------------------+----------------+
4 rows in set (0.06 sec)

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

1 participant