Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Fluent Model with String Enum

Tags:

vapor

fluent

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

like image 241
Vigneshkumar G Avatar asked Nov 14 '18 18:11

Vigneshkumar G


1 Answers

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 conform to ReflectionDecodable.

like image 73
LoVo Avatar answered Oct 15 '22 18:10

LoVo