Closed uniformity-matters closed 4 years ago
How did you create the enum in the migration? There are docs on it at https://docs.vapor.codes/4.0/fluent/schema/#enum
I based it on the documentation you linked to.
return database.enum(Nationality.schema)
.case("german")
.case("french")
.case("spanish")
.create()
.flatMap { (nationality) -> EventLoopFuture<Void> in
database.schema(Artist.schema)
.id()
...
.field("nationality", nationality, .required)
.create()
}
Checking the database I can see a table _fluent_enums
where all the enums with all their cases I created show up.
I worked around the problem by saving strings to the database and then having a computed var to get the swift enum. I'm still interested in knowing what the problem is with my initial, cleaner approach.
Had the same issue, and instead of using strings (my first easy fix as well I must admit), I just created a migration for the enum itself, then in the migration for the type using the enum, I just read the enum, based on the doc.
struct CreatePostType: Migration {
func prepare(on database: Database) -> EventLoopFuture<Void> {
return database.enum("post_types")
.case("article")
.case("tutorial")
.case("code")
.create()
.transform(to: ())
}
func revert(on database: Database) -> EventLoopFuture<Void> {
return database.enum("post_types").delete()
}
}
struct CreatePost: Migration {
func prepare(on database: Database) -> EventLoopFuture<Void> {
return database.enum("post_types").read().flatMap { postTypes in
return database.schema("posts")
.id()
.field("url", .string, .required)
.field("title", .string, .required)
.field("type", postTypes, .required)
.unique(on: "url", name: "no_duplicate_url")
.create()
}
}
}
func revert(on database: Database) -> EventLoopFuture<Void> {
return database.schema("posts").delete()
}
}
I just tried to first create the enum in one migration and then read it in another as suggested. Unfortunately to no avail. Same problem - same error. The migrations run fine, but once I try to insert data programmatically the SQL INSERT INTO command fails.
Did it throw the same error? Can you show us how you write the data? And your model should use the @Enum wrapper instead of @Field.
And your model should use the @enum wrapper instead of @field. 🤦🏻♂️ That did the trick.
Thanks to both of you!
I get the following error message when trying to save a model that uses a string-backed enum with fluent and PostgresSQL.
I'm using Vapor 4 and this is how I added fluent and the postgres-driver as dependencies:
I created the enum like this:
and added it to my model like this:
The same operation works fine when using SQLite.