I have a table2 which has a composite primary key built from two columns, one of which is a foreign key from another table called table1, and the other is called position.
The table has a third column which is a foreign key from table3 and I want to update it if I come across a duplicate key of (table1_id and position). But instead, I get an error mentioned in the title. Here is the table schema for table2.
CREATE TABLE `table2` (
`table1_id` int NOT NULL,
`table3_id` int NOT NULL,
`position` smallint NOT NULL,
`created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`table1_id`,`position`),
KEY `table1_id` (`table1_id`),
KEY `table3_id` (`table3_id`),
CONSTRAINT `fk_table1_id_1` FOREIGN KEY (`table1_id`) REFERENCES `table1` (`id`) ON DELETE CASCADE,
CONSTRAINT `fk_table3_id` FOREIGN KEY (`table3_id`) REFERENCES `table3` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3
This is the table that I am trying to update. The error "Operand should contain 1 column(s)" occurs when I try to do "INSERT INTO ON DUPLICATE KEY UPDATE" statement which will update table3_id.
I tried the following statement:
INSERT INTO `table2`
(`table1_id`, `position`, `table3_id`)
VALUES
(3275027, 1, 5711)
ON DUPLICATE KEY UPDATE
`table3_id` = VALUES(`table3_id`)
A record with table1_id = 3275027 and position = 1 already exists, I just want to change the table3_id but it won't let me.
I expect the record with a unique composite primary key (3275027, 1) which is (table1_id, position) to update the third column table3_id from one value to 5711.
I searched so much to find the answer and explanation for this, but I just couldn't eventually find a proper answer.
EDIT 1:
I have also tried to update the column manually with:
UPDATE table2 SET `table3_id` = 5711 WHERE `table1_id` = 3275027 AND position = 1;
but the same error occurs.
UPDATE:
I found out that i had two string needed to be concatenated put in parentheses without the "CONCAT" keyword to indicate calling the concat() function. That was happening in a trigger after the update.