找不到基本表或视图:1146表'digitalsalesportal.plan_agents'不存在

时间:2019-04-24 20:37:24

标签: mysql laravel

我想用两个独立的数据库连接创建一个belongsToMany桥表关系。我能够在本地环境中成功完成此操作,但是在暂存服务器中运行迁移时,迁移会大声说该表在所选数据库中不存在。

这是完整的错误:Base table or view not found: 1146 Table 'digitalsalesportal.plan_agents' doesn't exist (SQL: delete from `digitalsalesportal`.`plan_agents` where `agent_id` = 185 and `plan_id` in (5))

  • 根据登台服务器中的MySQLWorkbench,表plan_agents存在于数据库中
  • mysqlproposal的数据库连接正常工作
  • 这适用于本地环境
  • 模式digitalsalesportal存在于登台服务器中
  • 在登台服务器中的MySQLWorkbench中运行SQL查询delete from `digitalsalesportal`.`plan_agents` where `agent_id` = 185 and `plan_id` in (5)正常工作

迁移文件

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use App\Plan;
use App\PlanAgent;

class ConvertSalesPortalPlansToProposalPlans extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $sales_portal_plans = DB::table('plans');
        $proposal_plans = Plan::withTrashed();
        foreach(PlanAgent::all() as $plan_agent){
            $plan = $sales_portal_plans->find($plan_agent->plan_id);
            if(!$plan){
                continue;
            }
            $proposal_plan = $proposal_plans->where('name', $plan->name)->first();
            if($proposal_plan){
                $agent = $plan_agent->agent;
                $agent->plans()->detach([$plan_agent->plan_id]);
                $agent->plans()->attach([$proposal_plan->id]);
            }else{
                $plan_agent->delete();
            }
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
    }
}

代理类

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Agent extends Model
{
    use SoftDeletes;

    #  ------------
    # | Attributes |
    #  ------------

    /*
     * Date attributes
     *
     * @var array
     */
    protected $dates = ['deleted_at', 'created_at', 'updated_at', 'onboarded_date'];

    /*
     * Mass-asignment attributes
     *
     * @var array
     */
    protected $fillable = ['phone', 'onboarded_date', 'website', 'agency_id'];

    #  ---------------
    # | Relationships |
    #  ---------------


    /*
     * Returns Plans associated with White Label Agent
     *
     * @return mixed
     */
    public function plans(){
        return $this->belongsToMany('App\Plan', env('DB_DATABASE').'.plan_agents')->using('App\PlanAgent')->withTimestamps();
    }

PlanAgent类

<?php

namespace App;

use Illuminate\Database\Eloquent\Relations\Pivot;

class PlanAgent extends Pivot
{
    #  ------------
    # | Attributes |
    #  ------------

    /*
     * Defines table name
     *
     * @var string
     */
    protected $table = 'plan_agents';

    /**
     * Sets connection
     *
     * @var string
     */
    protected $connection = 'mysql';

    /**
     * Fillable attributes
     *
     * @var array
     */
    protected $fillable = ['plan_id', 'agent_id'];

    /**
     * Agent relationship
     *
     * @return mixed
     */
    public function agent()
    {
        return $this->belongsTo('App\Agent', 'agent_id');
    }
}

计划课程

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Validation\Rule;
use Validator;

class Plan extends Model
{
    use SoftDeletes;

    #  ------------
    # | Attributes |
    #  ------------

    /**
     * Sets connection
     *
     * @var string
     */
    protected $connection = 'proposal';

    /**
     * Sets table name
     *
     * @var string
     */
    protected $table = 'plans';

    /*
     * Date attributes
     *
     * @var array
     */
    protected $dates = ['deleted_at', 'created_at', 'updated_at'];

    /*
     * Fillable attributes
     *
     * @var array
     */
    protected $fillable = ['name'];

}

迁移的目标是使用我们正在使用的第二个数据库的新计划ID更新旧计划ID,并删除找不到匹配计划的任何记录。

1 个答案:

答案 0 :(得分:0)

您正在查询当前尚不存在的数据库,很可能是在提供程序之一中查询的?

导入Schema门面:

use Illuminate\Support\Facades\Schema;

然后将您的代码包装在以下语句中:

if (Schema::hasTable('plan_agents')) {
    // do something
}

因为您的表位于本地,所以不会出现此错误,但是当您迁移空表时,将弹出此错误。在本地手动删除所有表,然后运行迁移以查看是否也收到此错误?如果是这样,请尝试以上答案,然后查看hasTable()的适合位置。

相关问题