SQLite.swift icon indicating copy to clipboard operation
SQLite.swift copied to clipboard

Column check constraints generate a syntax error

Open mlaster opened this issue 4 years ago • 1 comments

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

  • Include the SQLite.swift version, commit or branch experiencing the issue. 0.12.2
  • Mention Xcode and OS X versions affected.
  • Xcode 13.0, iOS 15.0
  • How do do you integrate SQLite.swift in your project?
    • manual

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<Int>("type")

mlaster avatar Aug 01 '21 21:08 mlaster

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?

jberkel avatar Jul 19 '22 12:07 jberkel