M
M
Maxim Kozhin2017-09-28 22:53:59
Yii
Maxim Kozhin, 2017-09-28 22:53:59

How to set primary key name via Yii2 migration?

There is an iis server
There is php7.1
There is a sqlsrv extension for mssql By default
I write migration like this

$this->createTable('tableName', [
  'id' => $this->primaryKey(10)->unsigned(),
  ...
]

As a result, the key is named like PK__tableName_character
set. Moreover, the character set is different.
Next, I have to arrange a full-text index, again through migration, but I cannot know the name of the key that needs to be specified.
Question:
How can I set an auto-increment primary key using migration methods so that it is compatible not only with mssql, but also with mysql, for example?
Or how to find out the name of an already existing primary key?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dalph, 2017-12-15
@dalph

it is easier to create a similar key through

['id' => Schema::TYPE_UPK]

it’s not entirely clear why you need a name, because the reverse migration will
dropTable('tableName')

in mysql - the name of the key always matches the name of the field:
PRIMARY KEY (`id`)

to get information on the name through a query, you can use the query below. But in mysql - it returns PRIMARY, maybe in mssql it will be what you need
SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA . KEY_COLUMN_USAGE where CONSTRAINT_SCHEMA='db_name' AND TABLE_NAME='table_name' AND CONSTRAINT_NAME="PRIMARY"

to use requests is used
$this->db->createCommand($sql = null, $params = [])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question