没有不值得去解决的问题,也没有不值得去学习的技术!

Model associations across database connections in Laravel 6

获取关联属性时报错:SQLSTATE[42S02]: Base table or view not found: 1146 Table 'object_wp.wp_theme_installation' doesn't exist

1. The correlation of the model Theme is as follows


    /**
     * 获取与主题关联的主题安装记录
     */
    public function themeInstallation()
    {
        return $this->hasOne('Modules\ThemeStoreDB\Entities\ThemeInstallation', 'wp_theme_id');
    }


2. The correlation of the model ThemeInstallation is as follows


    /**
     * 获取此主题安装所属 WordPress 的主题
     * @return BelongsTo
     */
    public function theme()
    {
        return $this->belongsTo('App\Models\Theme', 'wp_theme_id');
    }


3. Error when obtaining the associated attribute: sqlstate[42S02]: base table or view not found: 1146 tableObject_wp.wp_theme_installationdoesn’tt exist . as shown in Figure 1

获取关联属性时报错:SQLSTATE[42S02]: Base table or view not found: 1146 Table 'object_wp.wp_theme_installation' doesn't exist
Figure 1

{
    "message": "SQLSTATE[42S02]: Base table or view not found: 1146 Table 'object_wp.wp_theme_installation' doesn't exist (SQL: select * from `wp_theme_installation` where `wp_theme_installation`.`wp_theme_id` = 105 and `wp_theme_installation`.`wp_theme_id` is not null limit 1)",
    "code": "42S02",
    "status_code": 500,
    "debug": {
        "line": 669,
        "file": "E:\\wwwroot\\object\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php",
        "class": "Illuminate\\Database\\QueryException",
        "trace": ...
    }
}


4. The table Theme_Installation does not exist in object_wp.wp_theme_installation, but actually exists in object_store.theme_installation

5. Set the connection to the connection where the table Theme_Installation is located in the association of the model Theme


    /**
     * 获取与主题关联的主题安装记录
     */
    public function themeInstallation()
    {
        return $this->setConnection('mysql')->hasOne('Modules\ThemeStoreDB\Entities\ThemeInstallation', 'wp_theme_id');
    }


6. Set the connection to the connection where the table Theme is located in the association of the model ThemeInstallation


    /**
     * 获取此主题安装所属 WordPress 的主题
     * @return BelongsTo
     */
    public function theme()
    {
        return $this->setConnection('wordpress')->belongsTo('App\Models\Theme', 'wp_theme_id');
    }


7. Obtain the associated attribute successfully. The result is printed as follows, if there is no corresponding record, it is NULL. as shown in Figure 2

获取关联属性成功。结果打印如下,如果不存在对应的记录,则为 NULL
Figure 2

NULL
object(Modules\ThemeStoreDB\Entities\ThemeInstallation)#3984 (26) {
  ["table":protected]=>
  string(18) "theme_installation"
  ["attributes":protected]=>
  array(11) {
    ["id"]=>
    int(2)
    ["theme_store_theme_id"]=>
    int(9)
    ["theme_id"]=>
    string(36) "96653e3a-9f95-44dd-ade9-d928a59d0332"
    ["wp_theme_id"]=>
    int(104)
    ["theme_name"]=>
    string(8) "theme"
    ["theme_custom_name"]=>
    string(8) "theme"
    ["role"]=>
    string(11) "unpublished"
    ["processing"]=>
    int(0)
    ["processing_failed"]=>
    int(1)
    ["created_at"]=>
    string(19) "2022-05-27 09:43:18"
    ["updated_at"]=>
    string(19) "2022-05-27 09:43:22"
  }
  ["fillable":protected]=>
  array(1) {
    [0]=>
    string(8) "theme_id"
  }
  ["connection":protected]=>
  string(5) "mysql"
  ["primaryKey":protected]=>
  string(2) "id"
  ["keyType":protected]=>
  string(3) "int"
  ["incrementing"]=>
  bool(true)
  ["with":protected]=>
  array(0) {
  }
  ["withCount":protected]=>
  array(0) {
  }
  ["perPage":protected]=>
  int(15)
  ["exists"]=>
  bool(true)
  ["wasRecentlyCreated"]=>
  bool(false)
  ["original":protected]=>
  array(11) {
    ["id"]=>
    int(2)
    ["theme_store_theme_id"]=>
    int(9)
    ["theme_id"]=>
    string(36) "96653e3a-9f95-44dd-ade9-d928a59d0332"
    ["wp_theme_id"]=>
    int(104)
    ["theme_name"]=>
    string(8) "theme"
    ["theme_custom_name"]=>
    string(8) "theme"
    ["role"]=>
    string(11) "unpublished"
    ["processing"]=>
    int(0)
    ["processing_failed"]=>
    int(1)
    ["created_at"]=>
    string(19) "2022-05-27 09:43:18"
    ["updated_at"]=>
    string(19) "2022-05-27 09:43:22"
  }
  ["changes":protected]=>
  array(0) {
  }
  ["casts":protected]=>
  array(0) {
  }
  ["dates":protected]=>
  array(0) {
  }
  ["dateFormat":protected]=>
  NULL
  ["appends":protected]=>
  array(0) {
  }
  ["dispatchesEvents":protected]=>
  array(0) {
  }
  ["observables":protected]=>
  array(0) {
  }
  ["relations":protected]=>
  array(0) {
  }
  ["touches":protected]=>
  array(0) {
  }
  ["timestamps"]=>
  bool(true)
  ["hidden":protected]=>
  array(0) {
  }
  ["visible":protected]=>
  array(0) {
  }
  ["guarded":protected]=>
  array(1) {
    [0]=>
    string(1) "*"
  }
}



8. The execution SQL is as follows


select
  *
from
  `theme_installation`
where
  `theme_installation`.`wp_theme_id` = 104
  and `theme_installation`.`wp_theme_id` is not null
limit
  1


 

需要长期技术维护或远程问题排查?

我是拥有 15+ 年经验的 PHP / Go 后端工程师,长期关注已有系统维护、Bug 修复、性能优化、服务器排查、WordPress 网站维护和小功能迭代。

如果你的项目遇到以下情况,可以先从一次小问题排查开始合作:

  • ✅ PHP / Laravel / Yii2 老项目无人维护
  • ✅ Go / Gin 后端接口需要排查或优化
  • ✅ WordPress 网站访问慢、报错或插件冲突
  • ✅ Nginx / MySQL / Redis / Linux 服务器异常
  • ✅ CDN / Cloudflare / DNS / HTTPS 配置问题
  • ✅ 需要长期远程技术支持或兼职维护

更多介绍请查看:关于我 & 合作

微信:13980074657
邮箱:shuijingwanwq@gmail.com
Telegram:@shuijingwan
GitHub:https://github.com/shuijingwan

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理