我正在编写一个Laravel应用程序,并且要在sqlite中进行测试,您需要为迁移中的所有字段指定 - > nullable()或 - >默认(“123”)。这让我感到困惑,因为我的用户迁移目前看起来像这样:
public function up() {
Schema::create('users', function($table) {
$table->increments('id');
$table->integer('role_id');
$table->string('username')->unique();
$table->string('password');
$table->string('name');
$table->string('email');
$table->integer('max_locations');
$table->string('auth_token');
$table->string('remember_token', 100);
$table->timestamps();
});
}
其中许多没有理由不添加 - >默认(“123”)和 - > nullable()。用户名是一个很好的例子,我不能将null作为数据库中的用户名,但如果我设置了一个不能正常工作的默认值。没有添加用户名的第二个用户将使用默认用户名,并且由于用户名必须是唯一的,因此会抛出异常。
目前我没有指定任何内容,SQL通过为所有这些字段提供默认值“None”来解决此问题。可以在某种程度上添加吗?如果没有,我的用户名是否应该是默认值,还是应该可以为空?
答案 0 :(得分:1)
您不能使用具有默认NULL值的NOT NULL列。如果您不希望列可以为空,只需指定默认值,如下所示:
$table->string('username')->unique()->default('');
在你的课程验证中,你不会让空值,所以没关系。
答案 1 :(得分:0)
尝试使用此
$table->string('username')->unique()->default($value);