更改哨兵2用户迁移表的主键

时间:2015-03-04 10:08:02

标签: php mysql laravel laravel-4 cartalyst-sentry

我已经尝试过这段代码,并通过这样做运行哨兵2附带的迁移: php artisan migrate:--package cartalyst/sentry. 我能够在我的数据库中创建用户,组和其他表。

如何将用户表的ID列从PRIMARY KEY更改为userID,我将其添加为迁移?

1 个答案:

答案 0 :(得分:0)

试试吧

使用php artisan migrate:make生成新的全新迁移,并在下面编写代码

public function up() {
    Schema::table('users', function(Blueprint $table) {
        $table->dropPrimary('users_id_primary');
        $table->integer("userID");
        $table->primary('userID');
    });
}

public function down() {
    Schema::table('users', function(Blueprint $table) {
        $table->dropPrimary('userID');
        $table->dropColumn('userID');
        $table->primary('users_id_primary');

    });
}

然后运行php artisan migrate

下一步并更改User模型

use Cartalyst\Sentry\Users\Eloquent\User as SentryUserModel;

class User extends SentryUserModel {

    protected $primaryKey = 'userID';
}

下一步

php artisan config:publish cartalyst/sentry

接下来打开app/config/packages/cartalyst/sentry中的配置文件并编辑

'users' => array(
 'model' => 'User',
 ...
 ),

希望它会有所帮助:)