P
P
Padre2018-02-05 22:16:51
Yii
Padre, 2018-02-05 22:16:51

Creating a table in Yii2?

The table with compound PRIMARY KEY, unique is necessary. The database query is:

CREATE TABLE `test33` (
    id_1 MEDIUMINT,
    id_2 MEDIUMINT,
    PRIMARY KEY (id_1, id_2)
)

I did:
$this->createTable('test33', [
            'id_1' => $this->primaryKey(),
            'id_2' => $this->primaryKey(),
        ]);

But this is transformed into a query below. It is clear that it is necessary to remove AUTO_INCREMENT from it for both columns, but how to register this in the migration?
'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
The SQL being executed was: CREATE TABLE `test3333` (
        `id_1` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
        `id_2` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY
)'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2018-02-05
@yii16

$this->createTable('test33', [
            'id_1' => $this->integer(11)->notNull(),
            'id_2' => $this->integer(11)->notNull(),
        ]);

$this->addPrimaryKey('pk-ids', 'test33, ['id_1', 'id_2']);

In this case, repetitions of matches are also excluded, that is, the pair id_1=55and id_2=66will be unique

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question