Skip to content

Using enums as dictionary keys #205

@CoolONEOfficial

Description

@CoolONEOfficial

Describe the bug

I can't use enums as dictionary keys.

To Reproduce

Example:

enum MyEnum: String, Codable {
    case one
}

final class MyModel: Model {
    static var schema: String  = "tests"

    @ID(key: .id)
    var id: UUID?

    @Field(key: "test")
    var test: [MyEnum : Float]
}

struct CreateMigration: Migration {
    func prepare(on database: Database) -> EventLoopFuture<Void> {
        return database.schema(MyModel.schema)
            .id()
            .field("test", .dictionary(of: .float), .required)
            .create()
    }

    func revert(on database: Database) -> EventLoopFuture<Void> {
        return database.schema(MyModel.schema).delete()
    }
}

let test = MyModel()
test.test = [.one: Float(1)]
test.save(on: app.db)

Steps to reproduce the behavior:

  1. Execute migration
  2. Execute MyModel creation
  3. See error "Array does not contain all: TEXT" at PostgresDataEncoder

Expected behavior

Create database schema correctly

Environment

  • Vapor Framework version: 4.35.0
  • Vapor Toolbox version: 18.3.0
  • OS version: macOS Big Sur 11.2.1 (20D74)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions