Chapter 6 Cloud Vapor Deploy Error

Hi, I am following the guide in chapter 6 and on the very last step when I’m testing out my endpoint
https://chen-wu-look-at-me.vapor.cloud/api/acronyms
I get this error:
{
“reason”: "Model.defaultDatabase is required to use request as DatabaseConnectable.",
“error”: true
}

Does anyone have any ideas? I just followed the code samples in the chapter.

Thank you

@0xtim Can you please help with this when you get a chance? Thank you - much appreciated! :]

@wuudles when you added the model to the migration config did you do migrations.add(migration:)? You need to use the migrations.add(model:). Or you forgot to add it to the list of migrations?

I’m sorry, i’m not sure i understand. what variable migrations are you referring to? I can post my configure.swift if that helps.

// 1
import FluentPostgreSQL
import Vapor

public func configure(
    _ config: inout Config,
    _ env: inout Environment,
    _ services: inout Services
    ) throws { // 2
    try services.register(FluentPostgreSQLProvider())
    let router = EngineRouter.default()
    try routes(router)
    services.register(router, as: Router.self)
    var middlewares = MiddlewareConfig()
    middlewares.use(ErrorMiddleware.self)
    services.register(middlewares)
    // Configure a database
    
    // 1
    var databases = DatabasesConfig()
    // 2
    let hostname = Environment.get("DATABASE_HOSTNAME")
        ?? "localhost"
    let username = Environment.get("DATABASE_USER") ?? "vapor"
    let databaseName = Environment.get("DATABASE_DB") ?? "vapor"
    let password = Environment.get("DATABASE_PASSWORD")
        ?? "password"
    // 3
    let databaseConfig = PostgreSQLDatabaseConfig(
        hostname: hostname,
        username: username,
        database: databaseName,
        password: password)
// 4
    let database = PostgreSQLDatabase(config: databaseConfig)
    // 5
    databases.add(database: database, as: .psql)
    // 6
    services.register(databases)
}

Yeah you’re missing a section! You need something that looks like this:

Use migrations.add(model:database:) for actual models and use migrations.add(migration:database:) for migrations that alter databases or seed data etc

2 Likes