Framework: allow user specified number in $table->tinyInteger('numbers') schema

Created on 30 Nov 2014  路  13Comments  路  Source: laravel/framework

I was unaware of the$table->boolean('confirmed'); helper [tinyint(1)] so I went ahead and did $table->tinyInteger('current', 1); for a column. I already have a $table->increments('id'); column. But when I migrated the new table for some reason because I passed a number to the tinyInteger helper it thought it was the primary key and threw an error for some reason.

``[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table def inition; there can be only one auto column and it must be defined as a key (SQL: create tableusers_workhistory(idint unsigned not null auto_inc rement primary key,user_idint unsigned not null,company_namevarchar (50) not null,positionvarchar(50) not null,start_datedate not null, end_datedate not null,currenttinyint not null auto_increment primar y key,created_attimestamp default 0 not null,updated_at` timestamp de
fault 0 not null) default character set utf8 collate utf8_unicode_ci)

I have since changed my current column to use the Boolean helper. Maybe this is how it works by design? I don't know. Just thought I post about it.

This is my migration that caused the error

``` php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateUsersWorkhistoryTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users_workhistory', function(Blueprint $table)
        {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->string('company_name', 50);
            $table->string('position', 50);
            $table->date('start_date');
            $table->date('end_date');
            $table->tinyInteger('current', 1);
            $table->timestamps();
        });
        Schema::table('users_workhistory', function(Blueprint $table)
        {
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');    
        });     
    }


    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users_workhistory', function(Blueprint $table)
        {
            $table->dropForeign('users_workhistory_user_id_foreign');
        });
        Schema::drop('users_workhistory');
    }

}

I have since changed $table->tinyInteger('current', 1); to $table->boolean('current');

Most helpful comment

$table->boolean('fieldname'); will give you TINYINT(1)

All 13 comments

    /**
     * Create a new tiny integer column on the table.
     *
     * @param  string  $column
     * @param  bool  $autoIncrement
     * @param  bool  $unsigned
     * @return \Illuminate\Support\Fluent
     */
    public function tinyInteger($column, $autoIncrement = false, $unsigned = false)
    {
        return $this->addColumn('tinyInteger', $column, compact('autoIncrement', 'unsigned'));
    }

Why are you giving tiny integer a length, the data type itself defines its length. The one on your

$table->tinyInteger('current', 1)

Gives the auto increment a value of true.

How I can create tinyInt column on the table with length = 1 like if I do
CREATE TABLE table1 (
column1 tinyint(1) DEFAULT NULL
)

Yes I also want to send the tiny integer length=1

I`d like do it too

$table->boolean('fieldname'); will give you TINYINT(1)

How can I set length and default value like this:

CREATE TABLE `tablename` (
   `fieldname1` tinyint(2) DEFAULT '3',
   `fieldname2` integer(3) DEFAULT '0',
   ...
)

Please help, thank you.

`
Schema::create('tablename', function (Blueprint $table)
{
    $table->addColumn('tinyInteger', 'fieldname1', ['lenght' => 2, 'default' => '3']);
    $table->addColumn('integer', 'fieldname2', ['lenght' => 3, 'default' => '0']);
})
`

@alex-qwerty-alex Thank you very much.

@alex-qwerty-alex
$table->addColumn('integer', 'created_time', [ 'length' => 22, 'default' => '0', 'comment'=>'created time' ] );
about laravel 5.4, the length not set success

Laravel 5.4 : Length Success, when unsigned added.

$table->addColumn('tinyInteger', 'notification_interval', ['lenght' => 2, 'default' => '30', 'unsigned' => true]);
$table->addColumn('integer', 'location_radius', ['lenght' => 4, 'default' => '500', 'unsigned' => true]);

in laravel 5.5
$table->addColumn('integer', 'location_radius', ['lenght' => 4, 'default' => '500', 'unsigned' => true]);
not working

While @iapparatus accomplishes the desired column type, it's coincidental we got a tinyint(1) when we really asked for a boolean. $table->boolean('fieldname') is an abstraction on the DBMS stating that you want a boolean. If the DBMS supports a true boolean, that's what you're going to get.

If you truly want a to have a field that gives you a range of 0-9, then using $table->boolean('fieldname') is risky. You'll want to follow @abdalovalex's example ($table->addColumn('tinyInteger', 'fieldname', ['length' => 1]);) to create a tinyint of a specific length. That way you're being explicit when telling Eloquent what you want, so you can be sure you get it.

$table->boolean('fieldname'); will give you TINYINT(1)

It works! Thanks.

Was this page helpful?
0 / 5 - 0 ratings