2015-08-21 3 views
14

मैं yii migrate भागने की कोशिश की, लेकिन यह निम्न त्रुटि से पता चला है: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 

यहाँ मेरी कोड है:

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; 
    } 
} 

तो Yii2 में प्रवासन का उपयोग करके समग्र प्राथमिक कुंजी बनाने के लिए कैसे?

उत्तर

16

अद्यतन

कोशिश Yii संस्करण 2.06 के रूप में आप नए स्कीमा निर्माता का उपयोग कर सकते हैं:

<?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']); 
... 
?> 

मूल जवाब

डॉन ' टी तालिका निर्माण में प्राथमिक कुंजी जोड़ने के लिए, केवल प्रकार की घोषणा:

use yii\db\Schema; 

,,, 

$this->createTable('news-cate', [ 
    'news-id' => Schema::TYPE_INTEGER . ' NOT NULL', 
    'cate-id' => Schema::TYPE_INTEGER . ' NOT NULL', 
]); 

उसके बाद आप इस तरह समग्र प्राथमिक कुंजी जोड़ सकते हैं: एकाधिक स्तंभों के लिए

$this->addPrimaryKey('news-cate_pk', 'news-cate', ['news-id', 'cate-id']); 

, सरणी addPrimaryKey() विधि में अनुमति दी है।

यह कच्चे वर्ग को लिखने से बेहतर है।

+1

यह गैर-पूर्णांक प्रकार की प्राथमिक कुंजी बनाने का सबसे अच्छा तरीका भी है। समाधान के लिए –

+1

धन्यवाद! –

7

इस तरह

public function safeUp() 
{ 
    $this->createTable('news-cate', [ 
     'news-id' =>'int NOT NULL', 
     'cate-id' =>'int NOT NULL', 
     'PRIMARY KEY (news-id,cate-id)' 
     ]); 
    $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"); 
} 
0

आदर्श रूप से आप दो तालिकाओं के लिए जंक्शन बना रहे हैं। तुम बस एक लाइनर कोड से पलायन बनाने के लिए Yii कमांड लाइन का उपयोग कर सकते हैं:

php yii migrate/create create_junction_table_for_post_and_tag_tables --fields="created_at:bigInteger" 

यह होगा उत्पन्न करता है:

/** 
* Handles the creation for table `post_tag`. 
* Has foreign keys to the tables: 
* 
* - `post` 
* - `tag` 
*/ 
class m160328_041642_create_junction_table_for_post_and_tag_tables extends Migration 
{ 
    /** 
    * @inheritdoc 
    */ 
    public function up() 
    { 
     $this->createTable('post_tag', [ 
      'post_id' => $this->integer(), 
      'tag_id' => $this->integer(), 
      'created_at' => $this->dateTime(), 
      'PRIMARY KEY(post_id, tag_id)', 
     ]); 

     // creates index for column `post_id` 
     $this->createIndex(
      'idx-post_tag-post_id', 
      'post_tag', 
      'post_id' 
     ); 

     // add foreign key for table `post` 
     $this->addForeignKey(
      'fk-post_tag-post_id', 
      'post_tag', 
      'post_id', 
      'post', 
      'id', 
      'CASCADE' 
     ); 

     // creates index for column `tag_id` 
     $this->createIndex(
      'idx-post_tag-tag_id', 
      'post_tag', 
      'tag_id' 
     ); 

     // add foreign key for table `tag` 
     $this->addForeignKey(
      'fk-post_tag-tag_id', 
      'post_tag', 
      'tag_id', 
      'tag', 
      'id', 
      'CASCADE' 
     ); 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function down() 
    { 
     // drops foreign key for table `post` 
     $this->dropForeignKey(
      'fk-post_tag-post_id', 
      'post_tag' 
     ); 

     // drops index for column `post_id` 
     $this->dropIndex(
      'idx-post_tag-post_id', 
      'post_tag' 
     ); 

     // drops foreign key for table `tag` 
     $this->dropForeignKey(
      'fk-post_tag-tag_id', 
      'post_tag' 
     ); 

     // drops index for column `tag_id` 
     $this->dropIndex(
      'idx-post_tag-tag_id', 
      'post_tag' 
     ); 

     $this->dropTable('post_tag'); 
    } 
} 

और तालिका संरचना इस तरह होगा:

+------------+------------+------+-----+---------+-------+ 
| Field  | Type  | Null | Key | Default | Extra | 
+------------+------------+------+-----+---------+-------+ 
| post_id | int(11) | NO | PRI | NULL |  | 
| tag_id  | int(11) | NO | PRI | NULL |  | 
| created_at | bigint(20) | YES |  | NULL |  | 
+------------+------------+------+-----+---------+-------+ 

कृपया जांच करें संदर्भ के लिए नीचे उल्लिखित यूआरएल:

http://www.yiiframework.com/doc-2.0/guide-db-migrations.html

संबंधित मुद्दे