5

I am trying to create a new migration for my users table, I have the following schema:

        Schema::create('users', function($t) {
            $t->increments('id');
            $t->string('username', 16);
            $t->string('password', 64);
            $t->integer('role', 64);
            $t->timestamps();
    });

When I try to run php artisan migrate from the terminal, I get the following error:

[Exception]
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 users (id int unsigne d not null auto_increment primary key, username varchar(16) not null, password varchar(64) no t null, role int not null auto_increment primary key, created_at timestamp default 0 not null , updated_at timestamp default 0 not null)) (Bindings: array (
))

The error has something to do with the 'role' field, as when this is removed it seems to run fine.

Thanks in advance for any help or insight.

1
  • Why does the generated SQL set role to auto_increment primary key? What's wrong with id being the only auto_increment primary key column? Commented Feb 18, 2013 at 8:07

3 Answers 3

13

The second parameter for integer is an auto increment flag.

public function integer($column, $autoIncrement = false, $unsigned = false)

https://github.com/laravel/framework/blob/5.4/src/Illuminate/Database/Schema/Blueprint.php#L510

4
  • @TheRabbitFactory, I know it is a while ago, but do you know what I need to do, because I have the same problem as you
    – Thom
    Commented Jun 1, 2014 at 14:12
  • I don't really remember but I think I was basically missing that second parameter. Commented Jun 1, 2014 at 14:25
  • 2
    it's pretty clear in this answer? you can't set size of an integer field
    – Robbo
    Commented Jun 2, 2014 at 15:11
  • MySQL's docs make clearer that the size for the integer data types are pre-set. When you look at CREATE TABLE code though, you will more than likely see something like INT(10), but this appears to be a conventional thing, since the max/min signed and unsigned value has 10 digits. Commented Feb 3, 2015 at 20:27
0
$t->integer('role', false);

That fixes it.

3
  • 4
    Can you explain why that fixes it?
    – Jason D
    Commented Jul 1, 2015 at 17:56
  • Answer without explanation are of no use. Commented Jul 2, 2015 at 10:13
  • by setting integer to false i disable autoIncrement this is why we bypass error. Commented Jul 30, 2015 at 5:47
0

Length attribute of integer is not permitted.Remove it and try.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.