sqlite.lua icon indicating copy to clipboard operation
sqlite.lua copied to clipboard

How to create a table with composite primary key using sqlite.lua?

Open JesperLundberg opened this issue 1 year ago • 1 comments

I'm trying to write a small scraping tool and I want to save based on the date and an identifier. So, for this a composite primary key is needed as I can have many rows with the same date and many rows with the same identifier but I want there to only be one for the combination of date and identifier.

I attempted: date = { "date", unique = true, primary = true }, appid = { "number", unique = true, primary = true }, but I get the error about there being two primary keys in the same table (which makes sense).

I cannot find how to create a composite key using this library. Is it possible or is that out of scope and I need to do the below?


I could do it using the "execute sql" and providing an sql string but I prefer not if something else is possible.

The following sql should create a composite key: CREATE TABLE something ( column1 INTEGER NOT NULL, column2 INTEGER NOT NULL, value, PRIMARY KEY ( column1, column2) );

JesperLundberg avatar Aug 14 '24 14:08 JesperLundberg

Small update. I managed to create the table using execute from sqlite.lua but now I can't access that table. sqlite.lua has no idea of how that table looks or that it even exists.

I found a function called schema that seems like it could be used to get the "connection" to sqlite.lua but... Not quite it seems?

JesperLundberg avatar Aug 15 '24 10:08 JesperLundberg