I am using Postgres 14 and writing an sql query so I can insert a new row into tag table:
| id | team_id | name |
|---|---|---|
| 2 | 4 | London On |
As simple as:
INSERT INTO tag (team_id, name) SELECT :teamId, :name;
$stmt->bindValue("teamId", 4, PDO::PARAM_INT);
$stmt->bindValue("name", 'London On', PDO::PARAM_STR);
$stmt->bindValue("teamMemberId", 428, PDO::PARAM_STR);
In tag table I have ManyToOne relation with team_member_tag table that should look like:
| id | team_member_id | tag_id |
|---|---|---|
| 314 | 428 | 2 |
I want to implement INSERT of the data into team_member_tag table when doing an insert into tag table.. I have all params needed as I wrote in an example.
Tried:
$stmt = $this->getConnection()->prepare(
'
INSERT INTO tag
(
team_id,
name
) VALUES (
:teamId,
:name,
) RETURNING tag.id
'
);
$stmt->executeQuery();
$stmt2 = $this->getConnection()->prepare('
INSERT INTO team_member_tag (team_member_id, tag_id)
VALUES
( :workspaceMemberId,
(select id from tag)
)
');
$stmt2->bindValue('workspaceMemberId', $workspaceMemberId);
$stmt2->executeQuery();
And now the error:
Cardinality violation: 7 ERROR: more than one row returned by a subquery used as an expression
Can you please help? Thanks