spatie / laravel-tags

Add tags and taggable behaviour to your Laravel app

Home Page:https://freek.dev/609-an-opinionated-tagging-package-for-laravel-apps

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Multi-DB inconsistency in accessing tagging tables

nargalzius opened this issue · comments

commented

So I followed the documentation on how to create/define your own tag model (to be able to use it in a multi DB setup)

I'm able to set the tags and retrieve them (I'm even able to do it with the model and the tagging tables on different DBs)

However when I try to SEARCH for them using the withAnyTags (or pretty much any search-related) method, it doesn't seem to use the tables as implemented in the adjustment I made.

To replicate:

I have 2 DBs: A and B. As I mentioned earlier, I've applied the adjustments and set the taggables and tags table to DB B

I was able to set and retrieve tags even from model/entities from DB A (which means it's correctly setting the tagging information on the right tables in B, even if we're tagging the model from A)

However, when I tried searching for tags (withAny, etc) for tags in that very same model in A I was able to set/sync/retrieve tags with, it threw an error because it was trying to access the tagging information from A as well, instead of B

commented

I'm pasting the actual error when trying to search for the tags from a model that's located in a different DB from the tags & taggables tables


Illuminate\Database\QueryException: SQLSTATE[42S02]: Base table or view not found:
1146 Table 'DB_A.tags' doesn't exist (SQL:

select *, CAST(MODEL.updated_at AS CHAR) as updated_at,
CAST(MODEL.created_at AS CHAR) as created_at from `MODEL`
where exists (select * from `tags` inner join `DB_B`.`taggables`
on `tags`.`id` = `DB_B`.`taggables`.`tag_id`
where `MODEL`.`uuid` = `DB_B`.`taggables`.`taggable_id`
and `DB_B`.`taggables`.`taggable_type` = App\MODEL
and `tags`.`id` in (2)) and `MODEL`.`deleted_at` is null)


Again to recap, the model (MODEL) is in DB_A, the tags and taggable tables are in DB_B

So everything in the select statement is actually correct - the model, that the taggables is taken from DB_B

The only thing wrong is the part where it was trying to access the tags table from DB_A (2nd line) - which I'm assuming it just assumed to be in the same DB as the model from which the tags relationship was invoked from.

It doesn't make sense though since the "override" of the tags method (which was in the documentation) already explicitly uses the new model for the tag (which I explicitly set to use DB_B). I even modified it to explicitly use the new tag (LocalTag)

public function tags(): MorphToMany
    {
        return $this
            ->morphToMany('App\LocalTag', 'taggable', $this->relationshipDBL('taggables'), null, 'tag_id')
            ->orderBy('order_column');
    }

relationshipDBL() is just my helper method to make sure it's explicitly prefixed with the right db (which in this case is DB_B

It's worth noting that searching for tags from a model in the same db (DB_B) works as intended, but I believe this is a genuine bug, because it's able to resolve separate model/tagging info when setting and retrieving - it only breaks when searching

Hi,

thanks for reporting this. As I'm not using this package in a multi-db context, I'm not going to invest time in fixing this.

Feel free to send a PR with tests that will fix this problem.