首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用eloquent创建表

使用eloquent创建表
EN

Stack Overflow用户
提问于 2018-08-12 15:50:07
回答 3查看 298关注 0票数 0

我正在尝试使用eloquent在Laravel中创建一个表。它将每个整数属性视为auto_increment primary key

另外,在创建表定义时,我是否可以声明字段是否为空。就像属性道布为空一样,我怎么能在这里把这个字段DOB声明为NULL呢?

模型代码块

代码语言:javascript
复制
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();
        });
    }

获取错误的原因:

代码语言:javascript
复制
 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

如何解决这两个问题?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-08-12 16:00:24

integer的第二个参数确定该列是否为自动增量字段。有关更多详细信息,请参阅here。因此,对于您的情况,像这样删除第二个参数,您可以通过链接可空方法使字段为空:

代码语言:javascript
复制
$table->integer('Category')

或者,如果您计划使用微整型,则可以使用以下命令:

代码语言:javascript
复制
$table->tinyInteger('Category')

如何使字段可为空

代码语言:javascript
复制
$table->string('DOB', 10)->nullable();
票数 1
EN

Stack Overflow用户

发布于 2018-08-12 17:13:40

代码语言:javascript
复制
   /**
     * 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中指定整数的大小。

代码语言:javascript
复制
  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();
        });

这可以很好地工作。

票数 1
EN

Stack Overflow用户

发布于 2018-08-12 15:55:16

尝试使用tinyInteger或仅移除整型字段的大小参数

代码语言:javascript
复制
$table->tinyInteger('Category');
// or
$table->integer('Category');
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51806829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档