I'm building a Trello clone and i have this struct, it´s a card, it goes into Boards, if you're familiar with a kanban you know what's what.
type Card struct {
Id int64
Title string
Description string
CheckList dll.DLL
CardLabels dll.DLL
}
Now to move this card from one board to another first it stores a reference to the initial board and when the destination board is chosen it runs this function:
func (p *Project) moveCard() {
boardFrom, err := p.project.Boards.GetAt(p.moveFrom[0])
if err != nil {
logger.Error.Fatal(err)
return
}
boardTo := p.getBoard()
c, err := boardFrom.(*kanban.Board).Cards.GetAt(p.moveFrom[1])
if err != nil {
logger.Error.Fatal(err)
return
}
cardVal := *c.(*kanban.Card)
storage.UpdateCardParent(cardVal.Id, boardTo.Id)
boardFrom.(*kanban.Board).Cards.RemoveAt(p.moveFrom[1])
boardTo.Cards.Append(&cardVal)
p.setLists()
}
on the front-end everything is fine and the card gets moved from one board to the other as expected, however saving to the DB storage.UpdateCardParent(cardVal.Id, boardTo.Id)
shows erratic behaviour in saving the new boardID and so on the next loadup, it loads the card in the board where it was erratically stored.
Constant is the fact that a card on the first board will always stay on the first board, no matter how much it's moved.
Other behaviours include:
Since i've confirmed that the issue is storing the values into DB i'm either getting the values wrong or the queries wrong, but i see no error on either. I've confirmed boards are created and cards are created with the correct parent id, this only happens when moving. The schema and query executed on update:
CREATE TABLE IF NOT EXISTS boards (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
project_id INTEGER NOT NULL REFERENCES projects(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS cards (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
card_desc TEXT,
board_id INTEGER NOT NULL REFERENCES boards(id) ON DELETE CASCADE
);
UPDATE cards
SET board_id = $2
WHERE id = $1
RETURNING *;
and the func that executes said query:
func UpdateCardParent(id int64, boardId int64) sql.Result {
stmt, err := DB.Prepare(UpdateCardParentSql)
if err != nil {
logger.Error.Fatal(ErrCreatSQLstmt, err)
}
res, err := stmt.Exec(id, boardId)
if err != nil {
logger.Error.Fatal(ErrExecSQLstmt, err)
}
return res
}
I see nothing wrong here, logs show correct values being passed to the SQL execution, am i just blind or looking in the wrong place entirely?
UPDATE:
Ok, upon further inspection i realize it's taking the query inputs in reverse, so it's actually taking a card with board_id and updating parent with card_id, issue still remains:
Am i an idiot that doesn't know how to query or is this somehow reversed? Because unless there's some weird Mandela effect stuff going on i can swear the update query is correct.
So it's a syntax issue, instead of using $
i should be using ?
for arguments.
So if instead of:
UPDATE cards
SET board_id = $2
WHERE id = $1
RETURNING *;
i have:
UPDATE cards
SET board_id = ?2
WHERE id = ?1
RETURNING *;
it works.
Guess i don't know how to query after all.