我正在尝试使用eloquent在Laravel中创建一个表。它将每个整数属性视为auto_increment primary key。
另外,在创建表定义时,我是否可以声明字段是否为空。就像属性道布为空一样,我怎么能在这里把这个字段DOB声明为NULL呢?
模型代码块
public function up()
{
Schema::create('devotees', function (Blueprint $table) {
$table->increments('id');
$table->integer('Category', 1);
$table->string('NAME',150);
$table->integer('GENDER',1);
$table->string('BGROUP',4);
$table->string('DOB',10);
$table->timestamps();
});
}获取错误的原因:
Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key (SQL: create table `devotees` (`id` int unsigned not null auto_increment primary key, `Category` int not null auto_increment primary key, `NAME` varchar(150) not null, `GENDER` int not null auto_increment primary key, `BGROUP` varchar(4) not null, `DOB` varchar(10) not null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')
at C:\xampp\htdocs\laravel\lsapp\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error
661| // message to include the bindings with SQL, which will make this exception a
662| // lot more helpful to the developer instead of just the database's errors.
663| catch (Exception $e) {
> 664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667| }
668|
Exception trace:
1 PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key")
C:\xampp\htdocs\laravel\lsapp\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458
2 PDOStatement::execute()
C:\xampp\htdocs\laravel\lsapp\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458如何解决这两个问题?
发布于 2018-08-12 16:00:24
integer的第二个参数确定该列是否为自动增量字段。有关更多详细信息,请参阅here。因此,对于您的情况,像这样删除第二个参数,您可以通过链接可空方法使字段为空:
$table->integer('Category')或者,如果您计划使用微整型,则可以使用以下命令:
$table->tinyInteger('Category')如何使字段可为空
$table->string('DOB', 10)->nullable();发布于 2018-08-12 17:13:40
/**
* Create a new integer (4-byte) column on the table.
*
* @param string $column
* @param bool $autoIncrement
* @param bool $unsigned
* @return \Illuminate\Support\Fluent
*/
public function integer($column, $autoIncrement = false, $unsigned = false)
{
return $this->addColumn('integer', $column, compact('autoIncrement', 'unsigned'));
}这是Blueprint.php的integer()函数。正如您所看到的,它在这里需要一个布尔参数。看起来您正在尝试为大小添加一个参数。您不能在Laravel中指定整数的大小。
Schema::create('devotees', function (Blueprint $table) {
$table->increments('id');
$table->integer('Category');
$table->string('NAME',150);
$table->integer('GENDER');
$table->string('BGROUP',4);
$table->string('DOB',10);
$table->timestamps();
});这可以很好地工作。
发布于 2018-08-12 15:55:16
尝试使用tinyInteger或仅移除整型字段的大小参数
$table->tinyInteger('Category');
// or
$table->integer('Category');https://stackoverflow.com/questions/51806829
复制相似问题