Javascript Laravel 5.2-将主键设置为电子邮件,而不是';id'; Schema::create('users',函数(Blueprint$table){ $table->primary('email'); $table->string('name'); $table->string('image_url'); $table->string('signin_with'); });
上面是我的数据库模式 这是我的用户模型Javascript Laravel 5.2-将主键设置为电子邮件,而不是';id'; Schema::create('users',函数(Blueprint$table){ $table->primary('email'); $table->string('name'); $table->string('image_url'); $table->string('signin_with'); });,javascript,php,ajax,laravel,laravel-5.2,Javascript,Php,Ajax,Laravel,Laravel 5.2,上面是我的数据库模式 这是我的用户模型 Schema::create('users', function (Blueprint $table) {<br> $table->primary('email');<br> $table->string('name');<br> $table->string('image_url');<br>
Schema::create('users', function (Blueprint $table) {<br>
$table->primary('email');<br>
$table->string('name');<br>
$table->string('image_url');<br>
$table->string('signin_with');<br>
});
主键用于链接到其他表,字符串比较比int比较慢
如果将用户信息存储在多个表中,则用户表的外键将是电子邮件地址
这意味着您要多次存储电子邮件地址。
因此,不要使用电子邮件作为主键,而是使用自动递增的主键。主键用于链接到其他表,字符串比较比int比较慢
如果将用户信息存储在多个表中,则用户表的外键将是电子邮件地址
这意味着您要多次存储电子邮件地址。
因此,不要使用电子邮件作为主键,而是使用自动递增的主键。在用户模式中:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Users extends Model
{
protected $table = 'users';
public $incrementing = false;
protected $primaryKey = 'email';
protected $fillable = [
'email', 'name' ,'image_url', 'signin_with'
];
}
在需要用户的模型中
$table->increments('id');
$table->string('email')->unique();
在索引字段上有一个外键。对于Users表,不需要在此字段上具有主键
基于用户模式中的:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Users extends Model
{
protected $table = 'users';
public $incrementing = false;
protected $primaryKey = 'email';
protected $fillable = [
'email', 'name' ,'image_url', 'signin_with'
];
}
在需要用户的模型中
$table->increments('id');
$table->string('email')->unique();
在索引字段上有一个外键。对于Users表,不需要在此字段上具有主键
基于您可能不想将电子邮件设置为主键
。很可能您希望将其设置为唯一
并向其添加索引
,因为主键也用于增量目的。我可能错了,但SQLState[42S01]向我建议该表已经存在。您可以发布完整的错误消息吗?我不希望电子邮件id为空,因为我可以将其分配给unique
,然后它将只是该表中的一个电子邮件条目,但它也可以为空,这是我不希望看到的。我需要另一个表中的电子邮件id,如外键
和复合键
。错误为[Illumb\Database\QueryException]SQLSTATE[42S01]:基表或视图已存在:1050您可能不想将电子邮件设置为主键。很可能您希望将其设置为唯一
并向其添加索引
,因为主键也用于增量目的。我可能错了,但SQLState[42S01]向我建议该表已经存在。您可以发布完整的错误消息吗?我不希望电子邮件id为空,因为我可以将其分配给unique
,然后它将只是该表中的一个电子邮件条目,但它也可以为空,这是我不希望看到的。我需要另一个表中的电子邮件id,如外键
和复合键
。错误为[Illumb\Database\QueryException]SQLSTATE[42S01]:基表或视图已存在:1050yes。。我将电子邮件存储在表中,因为电子邮件id将在其他表中使用,并且根据我们的要求,电子邮件id应该是主键。那我怎么解决呢?是的。。我将电子邮件存储在表中,因为电子邮件id将在其他表中使用,并且根据我们的要求,电子邮件id应该是主键。那我怎么解决呢?