Postgres connected vapor

Sis Ccr
1 min readOct 1, 2021

Well this is my first time trying out vapor. I am just starting out for crud operation. I have a post api that sends us to create function of the controller.

vapor
func create(req: Request) throws -> EventLoopFuture<Room> {
let room = try req.content.decode(Room.self)
return room.create(on: req.db).map { room }
}

but while creating the models I get this error returned

server: id of relation violates not-null constraint (ExecConstraints)

I had fluent models all setup

final class Room: Codable, Model, Content {
static let schema: String = "rooms"
@ID(custom: "id")
var id: Int?
........

and in migration:

func prepare(on database: Database) -> EventLoopFuture<Void> {
return database.schema("rooms")
.id()

using default key as id, gives us id like 34cfg-rsdflkgj-687dsfasdf,

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

I needed custom id as int, hence I used @ID(custom: “id”)

but turns out if we added custom IDtype from fluent, we need to define custome id type in our migration too. otherwise just using id() in our migration will make our db id type as uuid

changing the migration id type like this, now the types of id of fluent model and the data type in postgres db are same.

.field(“id”, .int, .identifier(auto: true))

--

--