skip to Main Content

Code:

` public function up()
    {
        Schema::create('subscriptions', function (Blueprint $table) {
            $table->id();
            $table->integer('month',4);
            $table->integer('price',7);
            $table->tinyInteger('status')->default(1);
            $table->timestamps();
        });
    }`

Error:
QLSTATE[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 subscriptions (id bigint unsigned not null auto_increment primary key, month int
not null auto_increment primary key, price int not null auto_increment primary key, status tinyint not null default ‘1’, created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate ‘utf8mb4_unicode_ci’)

3

Answers


  1. Chosen as BEST ANSWER

    Solution: I solved my error doing this because we can't set a size on integers.

     $table->integer('month');
     $table->integer('price');
    

  2. The second parameter in integer is a boolean.

    If true the field is autoincrement

    Remove 4 and 7 from integer.

    public function up()
        {
            Schema::create('subscriptions', function (Blueprint $table) {
                $table->id();
                $table->integer('month');
                $table->integer('price');
                $table->tinyInteger('status')->default(1);
                $table->timestamps();
            });
        }
    
    Login or Signup to reply.
  3. As Francesco Gallo said the Second parameter in an integer is a boolean.
    and if you are working with a price I highly recommend using a decimal instead of an integer.

    so it would be better to write it like below:

    
    public function up()
        {
            Schema::create('subscriptions', function (Blueprint $table) {
                $table->id();
                $table->integer('month');
                $table->decimal('price',5,3);
                $table->tinyInteger('status')->default(1);
                $table->timestamps();
            });
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search