Matheus Martins Matheus Martins -4 years ago 89
Swift Question

string primary key with vapor + fluent

I would like to have a table with a string column as a primary key without having to use raw SQL syntax.

Here's my fluent "preparation":

static func prepare(_ database: Database) throws {
try database.create("roles") { roles in
roles.id("name")
roles.string("readable_name")
}
}


According to both my tests and the docs, resulting query will be similar to:

CREATE TABLE `roles` (`name` INTEGER PRIMARY KEY NOT NULL, `readable_name` TEXT NOT NULL)


I could not, so far, find a way to have a string (TEXT, VARCHAR, ...) as a primary key without raw SQL syntax and i would like to know whether it's possible to do it or not using the fluent query builder which comes with vapor.

Answer Source

Support for ID types besides INT was added in Fluent 2.

https://docs.vapor.codes/2.0/fluent/model/#id-type

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download