雄辩地创建表

时间:2018-08-12 07:50:07

标签: php laravel laravel-5 eloquent

我试图在Laravel中使用雄辩的方法创建表。它将每个整数属性都视为auto_increment primary key

也可以在创建表定义时声明该字段是NULL还是NOT NULL。就像属性DOB为NULL一样,如何从此处将字段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

我该如何解决这两个问题?

3 个答案:

答案 0 :(得分:1)

1.75 -3.75 的第二个参数确定列是否为自动递增字段。有关更多详细信息,请参见here。因此,对于您的情况,像这样删除第二个参数,然后可以通过将其链接为可空方法来使字段为空:

chmod -R 777 storage

或者,如果您打算使用小整数,则可以使用以下代码:

php artisan config:clear

如何使字段为空

integer

答案 1 :(得分:1)

   /**
     * 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()函数。如您所见,它在这里需要一个布尔参数。似乎您正在尝试为size添加参数。您无法在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();
        });

这很好。

答案 2 :(得分:0)

尝试使用tinyInteger或只删除整数字段的大小参数

$table->tinyInteger('Category');
// or
$table->integer('Category');
相关问题