【问题标题】:Migrating multiple databases with Laravel Artisan使用 Laravel Artisan 迁移多个数据库
【发布时间】:2016-12-09 19:05:59
【问题描述】:

我的命令 migrate:s 包含以下内容

public function handle()
{

    $sites = Sites::all();


    foreach($sites as $site)
    {
        $host       = $site->h;
        $database   = $site->d;
        $username   = $site->u;
        $password   = $site->p;

        $this->info('Trying to migrate ' . $database);

        Config::set('database.connections.mysql.host',      $host);
        Config::set('database.connections.mysql.database',  $database);
        Config::set('database.connections.mysql.username',  $username);
        Config::set('database.connections.mysql.password',  $password);

        //Reconnect with new credentials
        DB::reconnect('mysql');

        //Call the migration on the new connection
        $this->call('migrate');

        $this->info('Migrations complete for database ' . $site->d);
    }
}

第一个站点总是可以很好地迁移,但是在它继续尝试创建迁移表之后,尽管已经有一个已填充的迁移表。

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'migrations' already exists

数据库的凭据是正确的,我不知道为什么它在第一次迁移后尝试创建表。

【问题讨论】:

  • 您能在每个站点上查看/发布迁移表的内容吗?我想知道它是否不同步所以它认为它没有运行迁移来创建迁移表?另外,您使用的是哪个版本的 Laravel?
  • @Loren 5.1,迁移表已满,我已经删除了数据库,从全新安装开始,它总是在第一个运行。现在只是在玩,似乎由于某种原因它没有同步凭据。

标签: php database laravel database-migration laravel-artisan


【解决方案1】:

问题是设置被缓存,因此第一个连接正常工作,因此您需要使用清除连接

DB::purge('connectionName');

【讨论】:

    【解决方案2】:

    只是一个想法,可能是 ->call 函数以某种方式重新初始化了 Laravel 的一部分,或者至少这是我最好的猜测。如果您将每个站点作为数据库连接(即 mysql1、mysql2、mysql3 等),您可以尝试使用 --database 选项进行 migrate 运行迁移,看看是否有帮助?

    迁移命令有一个选择数据库连接的选项:

    https://github.com/laravel/framework/blob/5.1/src/Illuminate/Database/Console/Migrations/MigrateCommand.php

    【讨论】:

    • 试过了,结果还是和base table not found一样。
    猜你喜欢
    • 2017-06-11
    • 2014-06-05
    • 1970-01-01
    • 2018-11-01
    • 2018-04-04
    • 2016-03-30
    • 2013-10-28
    • 2016-09-23
    • 2021-08-21
    相关资源
    最近更新 更多