@@ -980,7 +980,7 @@ describe("sqlite.tbl", function()
980980
981981 describe (" string_index:" , function ()
982982 local kv = tbl (" kvpair" , {
983- key = { " text" , primary = true , required = true , unique = true },
983+ key = { " text" , primary = true , required = true , default = " none " },
984984 len = " integer" ,
985985 }, db )
986986
@@ -1038,6 +1038,13 @@ describe("sqlite.tbl", function()
10381038 }]
10391039 )
10401040 end )
1041+
1042+ it (" insert with 0 or true to skip the primary key value." , function ()
1043+ kv [true ] = { len = 5 }
1044+ eq (5 , kv .none .len )
1045+ kv [" " ] = { len = 6 }
1046+ eq ({ key = " none" , len = 6 }, kv :where { len = 6 })
1047+ end )
10411048 end )
10421049
10431050 describe (" number_index" , function ()
@@ -1080,8 +1087,45 @@ describe("sqlite.tbl", function()
10801087 limit = 2 ,
10811088 }]
10821089 )
1090+ t [0 ] = { name = " x" }
1091+ eq (" x" , t [t .last_id ].name )
1092+ end )
1093+ end )
1094+
1095+ describe (" Relationships" , function ()
1096+ local todos = tbl (" todos_indexer" , {
1097+ id = true ,
1098+ title = " text" ,
1099+ project = {
1100+ reference = " projects.title" ,
1101+ required = true ,
1102+ on_delete = " cascade" ,
1103+ on_update = " cascade" ,
1104+ },
1105+ }, db )
1106+
1107+ local projects = tbl (" projects" , {
1108+ title = { type = " text" , primary = true , required = true , unique = true },
1109+ deadline = { " date" , default = db .lib .date " now" },
1110+ }, db )
1111+
1112+ it (" create new table with default values" , function ()
1113+ projects .neovim = {}
1114+ eq (" string" , type (projects .neovim .deadline ))
1115+ projects [" sqlite" ] = {}
1116+ --- TODO: if you have sqilte.lua todos[2] return empty table
1117+ end )
1118+
1119+ it (" fails if foregin key doesn't exists" , function ()
1120+ eq (
1121+ false ,
1122+ pcall (function ()
1123+ todos [2 ].project = " ram"
1124+ end )
1125+ )
10831126 end )
10841127 end )
1128+
10851129 -- vim.loop.fs_unlink(db_path)
10861130 end )
10871131
0 commit comments