6

Here is my Fluent Model

struct Ailment: PostgreSQLModel {

enum Frequency: String , Content {
    case regular = "Regular"
    case occasional = "Occasional"
    case incidentFound = "Incident Found"
}

var id: Int?

var ailment: String

var frequency: Frequency

var dateIdentified: Date?

var underMedication: Bool

var breifDescription: String

}

I am able to create Fluent Model with Int Enum, but it does not work with String Enum,

I am getting below Exception

Fatal error: Error raised at top level: ⚠️ DecodingError: Cannot initialize Frequency from invalid String value 1

Thanks in advance :)

marc-medley
  • 8,931
  • 5
  • 60
  • 66
Vigneshkumar G
  • 328
  • 1
  • 2
  • 9

1 Answers1

3

Referring this Issue it works with Vapor 3.0.6 and Fluent 3.0.0. The table should be created correct if your enum is conform to ReflectionDecodable.

LoVo
  • 1,856
  • 19
  • 21
  • 2
    I am having the same issue and came across your answer. How exactly do you conform to ReflectionDecodable if the enum has more then 2 cases? – Kevin Renskers Mar 17 '19 at 14:37
  • 1
    @KevinRenskers just check example: `Enums with two or more cases can conform. enum Pet { case cat, dog } extension Pet: ReflectionDecodable { static func reflectDecoded() -> (Pet, Pet) { return (.cat, .dog) } }` – LoVo Mar 18 '19 at 08:26
  • It say two OR MORE, just add the case to your enum. The `reflectDecoded()` just checks if two cases are the same or not. `enum Pet { case cat, dog, mouse } extension Pet: ReflectionDecodable { static func reflectDecoded() -> (Pet, Pet) { return (.cat, .dog) } }` – LoVo Mar 18 '19 at 08:57
  • 1
    So what if I have 4 cases, then how does `reflectDecoded` work since it's only checking two cases, and returning the first 2? Sorry for the questions, I am just not grokking this part :) – Kevin Renskers Mar 18 '19 at 09:08
  • It returns a tuple containing two distinct instances for your enum case type. That doesn`t mean that it only work with the two cases used in the method. – LoVo Mar 18 '19 at 09:37