yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

SQLite Migrate Exception: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY #11527

Closed larryli closed 8 years ago

larryli commented 8 years ago

What steps will reproduce the problem?

  1. Use 'dsn' => 'sqlite:' . __DIR__ . '/../runtime/database.sqlite', in config/db.php on yii2-app-basic.
  2. Create a migration as:

    use yii\db\Migration;
    
    class m160509_093358_foobar extends Migration
    {
    public function up()
    {
       $this->createTable('foobar', [
           'id' => $this->bigPrimaryKey(),
       ]);
    }
    
    public function down()
    {
       $this->dropTable('foobar');
    }
    }
  3. Run ./yii migrate

    What is the expected result?

*** applying m160509_093358_foobar
    > create table foobar ...Exception 'yii\db\Exception' with message 'SQLSTATE[HY000]: General error: 1 AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY
Failed to prepare SQL: CREATE TABLE `foobar` (
        `id` bigint PRIMARY KEY AUTOINCREMENT NOT NULL
)'

in C:\Users\larry\foobar\vendor\yiisoft\yii2\db\Command.php:230

Error Info:
Array
(
    [0] => HY000
    [1] => 1
    [2] => AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY
)

What do you get instead?

Revert af75262541e83f353a36baf888b3392c9d34f201 changes

Additional info

Q A
Yii version 2.0.8
PHP version 7.0.6
Operating system Windows 10
samdark commented 8 years ago

@larryli how would you solve this issue?

larryli commented 8 years ago

@samdark Revert af75262

cebe commented 8 years ago

need to add a comment that this is intentionally the same as normal integer. also need a test that breaks in case someone will change it,

SamMousa commented 8 years ago

@cebe Doesn't it make more sense to throw an exception?

If I make a migration I specify big integer for a reason; I wouldn't be too happy if you silently went ahead and created a normal integer. That could cause me to get into trouble later when I use the application.

I think failing early is better; if needed the implementor could then fallback to normal int primary key by catching the exception.

rob006 commented 8 years ago

If I make a migration I specify big integer for a reason; I wouldn't be too happy if you silently went ahead and created a normal integer. That could cause me to get into trouble later when I use the application.

What kind of trouble? SQLite INTEGER can store signed 8-bytes integer, same as MySQL BIGINT.

SamMousa commented 8 years ago

Ah, my bad. I thought it was about implicitly using another type that has a smaller domain. :8ball:

dynasource commented 8 years ago

My 2 suggestions:

samdark commented 8 years ago

https://github.com/yiisoft/yii2/pull/11653