Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand SQLDataType options #65

Open
kerusan opened this issue Oct 18, 2019 · 6 comments
Open

Expand SQLDataType options #65

kerusan opened this issue Oct 18, 2019 · 6 comments
Labels
enhancement New feature or request
Projects

Comments

@kerusan
Copy link

kerusan commented Oct 18, 2019

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).

@tanner0101 tanner0101 added the enhancement New feature or request label Oct 18, 2019
@tanner0101 tanner0101 added this to To Do in Vapor 4 via automation Oct 18, 2019
@tanner0101
Copy link
Member

tanner0101 commented Oct 18, 2019

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.

@kerusan
Copy link
Author

kerusan commented Oct 21, 2019

+1

@kerusan
Copy link
Author

kerusan commented Oct 23, 2019

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

@HagvallDataAB
Copy link

+1

@johan-carlberg
Copy link
Contributor

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()

@tanner0101 tanner0101 changed the title The type VARCHAR is not a type in the SQL generation Expand SQLDataType options Apr 22, 2020
@fatto
Copy link

fatto commented Aug 19, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Vapor 4
  
Backlog
Development

No branches or pull requests

5 participants