Laravel 5.2 数据库模式自行创建一个唯一列
Laravel 5.2 database scheme creates an unique column by it self
我有以下用于创建地址的架构 table:
Schema::create('addresses', function (Blueprint $table) {
$table->string('id')->index();
$table->string('street', 100);
$table->integer('number', 5);
$table->string('addition', 10);
$table->string('postal_code', 7);
$table->string('place', 45);
$table->string('country', 45);
$table->timestamps();
$table->softDeletes();
});
出于安全原因,'id' 是随机生成的唯一字符串,而不是自动递增整数。
只有一个问题:Laravel 使列 'number' 唯一,因为它是唯一具有整数数据类型的列。我们希望列 'id' 作为主键和唯一键。
我们也试过这个:
$table->primary('id')->index();
$table->uuid('id')->index();
$table->string('id')->primary()->index();
我仍然收到此错误:
Integrity constraint violation: 19 UNIQUE constraint failed:
addresses.number
我确实遇到了这个问题。查看这篇文章:http://garrettstjohn.com/article/using-uuids-laravel-eloquent-orm/
几乎正在发生的事情是 Laravel "says" 他们支持 UUID,但他们确实需要帮助。
您的架构会起作用,但为了确定起见,我这样使用它:
$table->primary('id');
使用本文提供的示例后,您应该有类似的东西(这是我的用户模型):
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Authenticatable
{
// UuidForKey is a custom trait added in the app folder
use SoftDeletes, UuidForKey;
// This disabled the auto-incrementing
public $incrementing = false;
// Make sure id is set as primary
protected $primaryKey = "id";
// Makes sure that the id is a string and not an integer
protected $casts = [
'id' => 'string',
];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'firstname',
'lastname',
'email',
'password',
'role',
'active',
];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
这对我有用:
Schema::create('addresses', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->integer('number', false);
});
我有以下用于创建地址的架构 table:
Schema::create('addresses', function (Blueprint $table) {
$table->string('id')->index();
$table->string('street', 100);
$table->integer('number', 5);
$table->string('addition', 10);
$table->string('postal_code', 7);
$table->string('place', 45);
$table->string('country', 45);
$table->timestamps();
$table->softDeletes();
});
出于安全原因,'id' 是随机生成的唯一字符串,而不是自动递增整数。
只有一个问题:Laravel 使列 'number' 唯一,因为它是唯一具有整数数据类型的列。我们希望列 'id' 作为主键和唯一键。
我们也试过这个:
$table->primary('id')->index();
$table->uuid('id')->index();
$table->string('id')->primary()->index();
我仍然收到此错误:
Integrity constraint violation: 19 UNIQUE constraint failed:
addresses.number
我确实遇到了这个问题。查看这篇文章:http://garrettstjohn.com/article/using-uuids-laravel-eloquent-orm/
几乎正在发生的事情是 Laravel "says" 他们支持 UUID,但他们确实需要帮助。
您的架构会起作用,但为了确定起见,我这样使用它:
$table->primary('id');
使用本文提供的示例后,您应该有类似的东西(这是我的用户模型):
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Authenticatable
{
// UuidForKey is a custom trait added in the app folder
use SoftDeletes, UuidForKey;
// This disabled the auto-incrementing
public $incrementing = false;
// Make sure id is set as primary
protected $primaryKey = "id";
// Makes sure that the id is a string and not an integer
protected $casts = [
'id' => 'string',
];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'firstname',
'lastname',
'email',
'password',
'role',
'active',
];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
这对我有用:
Schema::create('addresses', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->integer('number', false);
});