-
First of all, thanks a lot for this plugin! I'm in the process of writing a neovim plugin using local tbl = require("sqlite.tbl") --- for constructing sql tables
local db = require("sqlite.db") --- for constructing sql databases
-- the metadata table
local tbl_metadata = tbl("metadata", {
id = true,
entry = {
type = "integer",
unique = true,
reference = "data.id",
on_update = "cascade",
on_delete = "cascade",
},
})
-- the data table
local tbl_data = tbl("data", {
id = true,
test = { "text", required = true, unique = true },
})
local tbls = {
uri = "~/test.sqlite3",
data = tbl_data,
metadata = tbl_metadata,
}
local data = db(tbls)
data.data:remove() -- to ensure tbl is empty
data.metadata:remove() -- to ensure tbl is empty
data.data:insert({ test = "test" })
data.metadata:insert({ entry = 1 })
data.data:remove({ id = 1 }) If I understand things correctly, this should create a data and metadata row (which it does, I checked), and it should then delete the data row and (because 'cascade') also delete the metadata row. For me, the metadata row is still left however. Am I doing something wrong? I'm very much a noob when it comes to SQL so maybe that's where I go wrong. I would be really thankful for any pointers. :) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 11 replies
-
Hey @jghauser , Sorry for the later replay. at first glance, everything looked okay, but after testing the code, I couldn't understand why it didn't work 😆. Luckily after reviewing the tests files on cascading, it turns you are missing pk key in id. Try Edit: referenced key can not have |
Beta Was this translation helpful? Give feedback.
Hey @jghauser , Sorry for the later replay. at first glance, everything looked okay, but after testing the code, I couldn't understand why it didn't work 😆. Luckily after reviewing the tests files on cascading, it turns you are missing pk key in id.
Try
data.id = { type = "integer", pk = true }
. pk enable a field to be treated as a primary key.Edit: referenced key can not have
unique = true
as in data.entry.unique