Ich versuchte yii migrate
, aber es zeigte die folgende Fehlermeldung auszuführen:Wie erstellt man zusammengesetzten Primärschlüssel mithilfe von Migration in Yii2?
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
Hier ist mein Code:
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;
}
}
So, wie durch die Verwendung Migration in Yii2 zusammengesetzten Primärschlüssel erstellen?
Dies ist auch die beste Möglichkeit, Primärschlüssel ohne Integer-Typ zu erstellen. –
danke für die Lösung! –