db-migration icon indicating copy to clipboard operation
db-migration copied to clipboard

problem with new migration 2.0.6

Open spiritdead opened this issue 10 years ago • 9 comments

hi all im trying this

'id' => Schema::TYPE_STRING . '(64) PRIMARY KEY',

but in the new migration i cant make this 'id' => $this->string(64)->primaryKey();

is a bug or this not exist in this case i cant define string to primary key ?

my suggestion is modify the primary key for support int and strings

$this->string()->primaryKey()->autoincrement(false);

or

$this->string->primaryKey($autoincrement = false);

i need this for the table sessions in this case

$this->createTable('session',[
            'id' => Schema::TYPE_STRING . '(64) PRIMARY KEY',
            'user_id' => Schema::TYPE_INTEGER,
            'ip' => Schema::TYPE_STRING,
            'is_trusted' => Schema::TYPE_BOOLEAN,
            'expire' => Schema::TYPE_INTEGER,
            'data' => Schema::TYPE_STRING,
            'controllerCurrent' => Schema::TYPE_STRING,
            'actionCurrent' => Schema::TYPE_STRING,
            'pageCurrent' => Schema::TYPE_STRING,
            'is_guest' => Schema::TYPE_BOOLEAN,
        ],$tableOptions);

in this case i don't need autoincrement,etc and the string is primary key

spiritdead avatar Aug 29 '15 07:08 spiritdead

primaryKey() method is a predefined format of a primary key in form of an auto incremental integer. if you need another form of primarykey you currently can not use the columnschemabuilder for it.

cebe avatar Aug 29 '15 19:08 cebe

@cebe of course for this i make this issue for make a enhacement for the version 2.0.7 -support primary key for string and integer (for now is integer only) -add a option for the autoincrement

spiritdead avatar Aug 29 '15 19:08 spiritdead

I don't think it's possible w/o breaking compatibility.

samdark avatar Aug 29 '15 19:08 samdark

@samdark i know , but this limite the creator table, not all table work using integer and primary key the best sample is the DB sessions (DBSession yii2)

spiritdead avatar Aug 29 '15 19:08 spiritdead

my suggestion is in the primary key define the type data, and if is autoincrement adding parameters $this->primaryKey(integer = true, autoincrement = true) or adding consts IS_INTEGER, IS_STRING etc

spiritdead avatar Aug 29 '15 19:08 spiritdead

@spiritdead, I think that your case isn't reason for enhancement. There are can be many other unique migration cases. Core team can't cover all custom cases.

Don't use columnschemabuilder for your case. Use custom definition for your PK column. Is it problem for you?

rugabarbo avatar Aug 30 '15 07:08 rugabarbo

@rugabarbo the standard PK is integer and string, i don't say other type of data, and the autoincrement is adding a parameter in the function primaryKey(), or you will limite this only autoincrement ?

and i show the default sample creating a migration for the table Sessions (this table is default in the core yii2 and this use String PK)

spiritdead avatar Aug 30 '15 08:08 spiritdead

:+1: IMO, its OK to add method primaryKey() to yii\db\ColumnSchemaBuilder. Current primaryKey() method are belong to yii\db\Migration.

'id' => $this->primaryKey(), // --> 'id' => Schema::TYPE_PK

'user_id' => $this->integer()->primaryKey(), // --> 'user_id' => Schema::TYPE_INTEGER . ' PRIMARY KEY'

PS: To avoid confusion we can rename yii\db\Migration::primaryKey() to yii\db\Migration::serial(), yii\db\Migration::squence() or something like that.

mdmunir avatar Aug 31 '15 07:08 mdmunir

The issue related with yiisoft/db, primaryKey is a part of Yiisoft\Db\Schema\Builder\AbstractColumn class.

https://github.com/yiisoft/db/blob/4f1dbb95c9551b4359cdb2b188d0094bd70aa138/src/Schema/SchemaInterface.php#L124

https://github.com/yiisoft/db-mysql/blob/d48ca3a6ad83abf6ed97e146185c76c5ce2e55ab/src/QueryBuilder.php#L20

And this should be solved there

Tigrov avatar Oct 16 '23 12:10 Tigrov

Done

Tigrov avatar Jan 08 '25 09:01 Tigrov