1
I’m making a basic system that has a table of projects
, one of tags
and a pivot table, project_tag
. Each project
has a id
, title
and a tag
. The table of tags
has a id
and a title
. The table projeto_tag
has a projeto_id
and a tag.id
, logo, each project has a table for each tag, thus making a relationship from many to many. Follow the tables below:
Project table:
CREATE TABLE projects (
user_id INTEGER,
id INTEGER NOT NULL
PRIMARY KEY AUTOINCREMENT,
title TEXT,
create_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (
user_id
)
REFERENCES users (id) ON DELETE CASCADE
ON UPDATE CASCADE
);
Tag table:
CREATE TABLE tags (
id INTEGER NOT NULL
PRIMARY KEY AUTOINCREMENT,
tag VARCHAR (255)
);
Table project_tag:
CREATE TABLE project_tag (
id INTEGER NOT NULL
PRIMARY KEY AUTOINCREMENT,
project_id INTEGER,
tag_id INTEGER,
FOREIGN KEY (
project_id
)
REFERENCES projects (id) ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (
tag_id
)
REFERENCES tags (id) ON DELETE CASCADE
ON UPDATE CASCADE
);
I want to filter projects based on the tags a user has passed, example:
['React.js', 'Knex', e por ai vai...]
,But, well, at least the way I’m selecting it, it’s returning one for each tag that it has, not all of the joints, which makes the Where process difficult, I think.
That said, I can’t make a where
that satisfies my will, which is to filter the projects based on the tags that I passed, for example: if I pass the tag "React.js", return all the projects that have this tag and also the others. I did some research, but I couldn’t find.
Observing: I am using the Knex in the back-end and my database is the Sqlite3. Code in SQL that is making the select and generating the return of the fourth image:
SELECT projects.id, tags.tag, tags.id FROM projects
INNER JOIN project_tag on project_tag.project_id = projects.id
INNER JOIN tags on project_tag.tag_id = tags.id
No database was informed, but already tried the clause
WHERE tags.tag IN("React.js", "Knex")
– Augusto Vasques
Already. The problem is that since it is returning several columns, one for each tag, although the project did not have the "Knex" tag, it would return the project with the "React" tag. Example: a project with the tags "React.js" and "Angular"
– Junior3874
Detail your data model by asking the SQL of the tables involved in the operation, the name of the database (Important to define the SQL syntax to be used) and if possible a sample of the data so we can replicate your problem.
– Augusto Vasques
I’ve given the question a redo to try to make it a little clearer, thank you.
– Junior3874
Where are the
CREATE TABLE
to replicate your problem?– Augusto Vasques
I refactored again, buddy
– Junior3874
@Junior3874, the way you put it, we would have to finger-type everything inside the images. With selectable text, we can just copy and paste in a test environment to reproduce your problem and try to help.
– Weslley Araújo
I changed it already, buddy
– Junior3874