Joeeee
Joeeee

Reputation: 1266

Yii migrations dropTable and FK

I have migration:

function Up:

$this->createTable(
            'vacancy_moderate',
            array(
                'id' => 'int(10) unsigned NOT NULL AUTO_INCREMENT',
                'period_days' => 'tinyint(4) NOT NULL',
                'title' => 'varchar(255) NOT NULL DEFAULT \'\'',
                'price'=> 'int(11) DEFAULT NULL',
                'requirements'=> 'text NOT NULL',
                'conditions'=> 'text',
                'contact_details'=> 'text NOT NULL',
                'country_id'=> 'int(10) unsigned NOT NULL',
                'city_id'=> 'int(10) unsigned DEFAULT NULL',
                'user_id'=> 'int(10) unsigned DEFAULT NULL',
                'club_id'=> 'int(10) DEFAULT NULL',
                'PRIMARY KEY (`id`)',
                'KEY `city_id` (`city_id`)',
                'KEY `user_id` (`user_id`)',
                'KEY `country_id` (`country_id`)',
                'KEY `club_id` (`club_id`)',
                'CONSTRAINT `vacancy_moderate_ibfk_1` FOREIGN KEY (`city_id`) REFERENCES `city` (`id`) ON UPDATE CASCADE',
                'CONSTRAINT `vacancy_moderate_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE SET NULL ON UPDATE CASCADE',
                'CONSTRAINT `vacancy_moderate_ibfk_3` FOREIGN KEY (`country_id`) REFERENCES `country` (`id`) ON UPDATE CASCADE',
                'CONSTRAINT `vacancy_moderate_ibfk_4` FOREIGN KEY (`club_id`) REFERENCES `clubs` (`id`) ON UPDATE CASCADE'
            ),
            'ENGINE=InnoDB DEFAULT CHARSET=utf8'
        );

function Down:

$this->dropTable('vacancy_moderate');

Question: Should I drop all foreign keys manually or they will be dropped within dropTable?

Upvotes: 0

Views: 1547

Answers (2)

tslater
tslater

Reputation: 4432

The Yii dropTable() function merely calls the MySQL command "DROP TABLE {table name}". MySQL will drop foreign keys when you drop the table.

public function dropTable($table)
{
    echo "    > drop table $table ...";
    $time=microtime(true);
    $this->getDbConnection()->createCommand()->dropTable($table);
    echo " done (time: ".sprintf('%.3f', microtime(true)-$time)."s)\n";
}

Upvotes: 0

acorncom
acorncom

Reputation: 5955

They'll drop with the table, as everything is going away. :-)

Upvotes: 1

Related Questions