Ben yii migrate
kaçmaya çalıştı, ancak aşağıdaki hatayı gösterdi: İşteYii2'de geçişi kullanarak bileşik birincil anahtar nasıl oluşturulur?
create table news-cate ...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
The SQL being executed was: CREATE TABLE `news-cate` (
`news-id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`cate-id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY
benim kodudur:
class m150821_083020_create_newscate_table extends Migration
{
public function safeUp()
{
$this->createTable('news-cate', [
'news-id' => $this->primaryKey(),
'cate-id' => $this->primaryKey(),
]);
$this->addForeignKey("fk_news_cate_nid", "news-cate", "news-id", "news", "id", "RESTRICT", "CASCADE");
$this->addForeignKey("fk_news_cate_cid", "news-cate", "cate-id", "category", "id", "RESTRICT", "CASCADE");
}
public function safeDown()
{
echo "m150821_083020_create_newscate_table cannot be reverted.\n";
$this->dropTable("news-cate");
return false;
}
}
Yani Yii2 Göç kullanarak bileşik birincil anahtar oluşturmak için nasıl?
<?php
use yii\db\Migration;
...
$this->createTable('news-cate', [
'news-id' => $this->integer()->notNull(),
'cate-id' => $this->integer()->notNull(),
]);
$this->addPrimaryKey('news-cate_pk', 'news-cate', ['news-id', 'cate-id']);
...
?>
ORİJİNAL CEVAP
Don':
Ayrıca, tamsayı olmayan türde birincil anahtarlar oluşturmanın en iyi yoludur. –
Çözüm için teşekkürler! –