Yii2控制台迁移到其他连接失败
我正在尝试将控制台迁移到其他connecton,但失败 就我而言,我有Yii2控制台迁移到其他连接失败,yii2,Yii2,我正在尝试将控制台迁移到其他connecton,但失败 就我而言,我有 'db' => [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=pos_db', ....other configs ], 'connection_identifier' => [ 'class' => 'yii\
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=pos_db',
....other configs
],
'connection_identifier' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=newdbo',
...other configs
],
在我的控制台里
public function init()
{
$this->db = 'connection_identifier';
parent::init();
}
public function safeUp()
{
$this->createTable('database_connection_domains', [
'id' => $this->primaryKey(),
'domain'=>$this->text()->notNull(),
'connection_id'=>$this->integer()->notNull(),
'created_at' => $this->integer()->notNull(),
'status'=>$this->integer()->defaultValue(0),
'FOREIGN KEY (connection_id) REFERENCES database_connections (id) ON DELETE RESTRICT ON UPDATE CASCADE',
]);
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropTable('database_connection_domains');
}
当我运行向上迁移时,数据库将在newdbo
数据库上正确创建。问题出现在down命令期间,在该命令中表不会被删除。我该怎么做才能把它扔到桌子上
当我运行/yii migrate/fresh
时,得到一个错误基表或视图已经存在:1050表“数据库连接”已经存在
,这意味着该表没有被删除
我遗漏了什么?
/yii migrate/fresh
不使用迁移来清理数据库,而是使用它只是以正确的顺序删除所有表。因此,您的DB组件设置从未使用过。您需要在命令调用时配置数据库:
./yii migrate/fresh --db=connection_identifier
我想你需要先放下外键?您是否收到任何错误?我收到的错误基表或视图已存在:1050表“database_connections”已存在。这意味着,当我运行/yii migrate/fresh时,表不会被删除。在所有表都被删除并被读取后,它会显示什么?当您运行
php yii migrate/down all
时,它会显示一条消息成功向下迁移。谢谢。当我运行yii migrate/down all时,它现在会删除其他数据库中的所有表,然后我可以使用yii migrate重新应用这些表。为什么在yii migrate/fresh过程中会失败,但它使用的是$this->db
,它具有应用迁移时要使用的db连接对象或db连接的应用程序组件ID的定义,还是我弄错了?@MuhammadOmerAslam我真的不明白你的意思。迁移不用于通过migrate/fresh
清理数据库,因此迁移中的每个设置都将被忽略。我刚才说的是您在回答中添加的自定义实现链接中truncateDatabase()
函数的前两行。truncateDatabase()
是在MigrateController
级别实现的,OP正在尝试在Migration
级别配置DB组件。这是两个独立的类。