stephencelis / SQLite.swift

A type-safe, Swift-language layer over SQLite3.
MIT License
9.59k stars 1.55k forks source link

Column check constraints generate a syntax error #1056

Open mlaster opened 2 years ago

mlaster commented 2 years ago

Issues are used to track bugs and feature requests. Need help or have a general question? Ask on Stack Overflow (tag sqlite.swift).

Build Information

General guidelines

Column check constraints seem to generate invalid SQL:

I'm creating a table with:

            let validTypeRange = 0...26

            try connection.writeConnection.savepoint {
                try connection.writeConnection.run(observations.create { builder in
                    builder.column(Expression.identifier)
                    builder.column(Expression.date)
                    builder.column(Expression.type, check: validTypeRange ~= Expression.type)
//                    builder.check(validTypeRange ~= Expression.type)
                })

And it generates a syntax error:

[logging] near ""type"": syntax error in "CREATE TABLE "Observations" ("identifier" BLOB NOT NULL, "date" TEXT NOT NULL, "type" INTEGER NOT NULL CHECK "type" BETWEEN 0 AND 26)"

If I use table checks instead of column checks, it works fine.

Expression.type is:

Expression("type")

jberkel commented 1 year ago

It looks like the parens are missing around CHECK:

"type" INTEGER NOT NULL (CHECK "type" BETWEEN 0 AND 26)

Can you paste the whole generated SQL by the table builder, before it gets executed?