vapor/sql-kit

Expand SQLDataType options

kerusan opened this issue ยท 6 comments

Code snippet

        try self.db.create(table: "galaxies")
            .column("id", type: .int, .primaryKey)
            .column("name", type: .text)
            .run().wait()

Generates SQL as

CREATE TABLE "galaxies"("id" INTEGER PRIMARY KEY DEFAULT UNIQUE, "name" TEXT)

The data type TEXT is not part of SQL 92, and is not recognized by Frontbase. There needs to be some way to declare or alias to a VARCHAR or CHARACTER VARYING type.

Vapor 3 was more flexible.

(Autoincrement is implemented in Frontbase by setting the default to the UNIQUE function).

You can pass any SQLExpression to the type and constraints fields:

try self.db.create(table: "galaxies")
    .column("id", type: SQLRaw("INTEGER"), SQLRaw("UNIQUE"))
    .column("name", type: SQLRaw("VARCHAR"))
    .run().wait()

That said, maybe we could have SQLDataType integrate with SQLDialect. Perhaps instead of having defaults like .int, .text, etc, we have SQLDialect attempt to return the best possible type for a given Swift type? I'm open to ideas here.

+1

The +1 is for solving it with ' SQLDataType integrate with SQLDialect'

You can pass any SQLExpression to the type and constraints fields:

try self.db.create(table: "galaxies")
    .column("id", type: SQLRaw("INTEGER"), SQLRaw("UNIQUE"))
    .column("name", type: SQLRaw("VARCHAR"))
    .run().wait()

This solution does work, although it needs to be expressed as either

try self.db.create(table: "galaxies")
    .column("id", type: .custom(SQLRaw("INTEGER")), .custom(SQLRaw("UNIQUE")))
    .column("name", type: .custom(SQLRaw("VARCHAR")))
    .run().wait()

or

try self.db.create(table: "galaxies")
    .column(SQLRaw("id"), type: SQLRaw("INTEGER"), SQLRaw("UNIQUE"))
    .column(SQLRaw("name"), type: SQLRaw("VARCHAR"))
    .run().wait()
fatto commented

SQLDataType goes through SQLDialect customDataType(for dataType: SQLDataType) function so this issue can be closed now I think.