# Chapter 31.4: Enums Tests UserType

**URL:** <https://forums.kodeco.com/t/chapter-31-4-enums-tests-usertype/138567>\
**Category:** Server Side Swift with Vapor\
**Created:** [April 25, 2021, 11:10am UTC](https://forums.kodeco.com/t/chapter-31-4-enums-tests-usertype/138567 "2021-04-25T11:10:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon60585370](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@anon60585370](https://forums.kodeco.com/u/anon60585370)\
**Post date:** [April 25, 2021, 11:10am UTC](https://forums.kodeco.com/t/chapter-31-4-enums-tests-usertype/138567/1 "2021-04-25T11:10:42Z")

</div>

I’ve been following the Server Side Swift Book and now I’m kinda stuck when adding the userType.  
It runs, but the tests won’t run anymore.  
I’ve tried to add it on my own by using what I learned, something seems to be missing (for me) tho.

This is the error I’m getting:  
`**error codes.vapor.application : database-id=psql psql_connection_id=D952CE7C-83B3-4902-9AF5-5CD7ED3C1CA9 psql_error=PSQLError(base: PostgresNIO.PSQLError.Base.server(PostgresNIO.PSQLBackendMessage.ErrorResponse(fields: [Severity: "ERROR", Message: "type \"userType\" already exists", Routine: "DefineEnum", File: "typecmds.c", Localized Severity: "ERROR", Line: "1189", Code: "42710"]))) Query failed**`

in Models+Testable.swift I added `userType: UserType = .standard,` in the create function and

```swift
let user = User(
      name: name,
      username: createUsername,
      password: password,
      email: "\(createUsername)@test.com",
      userType: userType)

```

in UserTests.swift I added  
`let usersUserType = UserType.standard` and updated `let user = try User.create(name: usersName, username: usersUsername, userType: usersUserType, on: app.db)`

I stopped and removed the docker container and I am starting it with

```auto
docker run --name postgres-test \
  -e POSTGRES_DB=vapor-test \
  -e POSTGRES_USER=vapor_username \
  -e POSTGRES_PASSWORD=vapor_password \
  -p 5433:5432 -d postgres:12-alpine

```

still having the error tho. Not sure how to fix this.

---

<div class="post-metadata">

**Author:** ![0xtim](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/0xtim/32/111111_2.png) [@0xtim](https://forums.kodeco.com/u/0xtim)\
**Post date:** [April 25, 2021, 3:30pm UTC](https://forums.kodeco.com/t/chapter-31-4-enums-tests-usertype/138567/2 "2021-04-25T15:30:35Z")

</div>

What migrations do you have and what do they look like? (Anything related to User and the enum)

---

<div class="post-metadata">

**Author:** ![anon60585370](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@anon60585370](https://forums.kodeco.com/u/anon60585370)\
**Post date:** [April 25, 2021, 10:13pm UTC](https://forums.kodeco.com/t/chapter-31-4-enums-tests-usertype/138567/3 "2021-04-25T22:13:41Z")

</div>

I followed the book for that and don’t think I’ve added anything else, just tried to include what I had from the chapters before.

```swift
import Fluent

struct CreateUser: Migration {
  func prepare(on database: Database) -> EventLoopFuture<Void> {
    database.enum("userType")
      .case("admin")
      .case("standard")
      .case("restricted")
      .create()
      .flatMap { userType in
        database.schema("users")

          .id()

          .field("name", .string, .required)
          .field("username", .string, .required)
          .field("password", .string, .required)
          .field("siwaIdentifier", .string)
          .field("email", .string, .required)
          .field("profilePicture", .string)
          .field("userType", userType, .required)

          .unique(on: "email")
          .unique(on: "username")

          .create()
      }
  }

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

extension User {
  enum v20210410 {
    static let schemaName = "users"
    static let id = FieldKey(stringLiteral: "id")
    static let name = FieldKey(stringLiteral: "name")
    static let username = FieldKey(stringLiteral: "username")
    static let password = FieldKey(stringLiteral: "password")
    static let email = FieldKey(stringLiteral: "email")
    static let siwaIdentifier = FieldKey(stringLiteral: "siwaIdentifier")
    static let profilePicture = FieldKey(stringLiteral: "profilePicture")
  }
  enum v20210411 {
    static let twitterURL = FieldKey(stringLiteral: "twitterURL")
  }
  enum v20210425 {
    static let userType = FieldKey(stringLiteral: "userType")
  }
}

```

---

<div class="post-metadata">

**Author:** ![0xtim](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/0xtim/32/111111_2.png) [@0xtim](https://forums.kodeco.com/u/0xtim)\
**Post date:** [April 26, 2021, 8:57am UTC](https://forums.kodeco.com/t/chapter-31-4-enums-tests-usertype/138567/4 "2021-04-26T08:57:31Z")

</div>

Right I see what’s happening. You’re not deleting the enum in your revert. So in the first test, it creates the enum, creates the user table, runs the test then deletes the user table. Then the next next, tries to create the enum again and fails because it hasn’t been removed. So you need to change your revert to delete the enum

---

<div class="post-metadata">

**Author:** ![anon60585370](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@anon60585370](https://forums.kodeco.com/u/anon60585370)\
**Post date:** [April 26, 2021, 11:06am UTC](https://forums.kodeco.com/t/chapter-31-4-enums-tests-usertype/138567/5 "2021-04-26T11:06:26Z")

</div>

Well, that sounds easier then it is for me. haha

```auto
func revert(on database: Database) -> EventLoopFuture<Void> {
    database.enum("userType").delete()
    database.schema("users").delete()
  }

```

when build shows the error

```auto
Missing return in a function expected to return 'EventLoopFuture<Void>'; did you mean to return the last expression?

```

I have no idea what do do with that information, I tried the fix of adding return before database.schema… but the end error is the same as before.

Edit:

the revert should look like this to not throw any errors

```swift
func revert(on database: Database) -> EventLoopFuture<Void> {
    database.schema("users").delete().flatMap {
      database.enum("userType").delete()
    }
  }

```

This works now, thanks @0xtim for the help!
