vapor-community / mysql-provider

MySQL provider for the Vapor web framework.
MIT License
30 stars 12 forks source link

Unable to use config file #18

Closed benjdum59 closed 7 years ago

benjdum59 commented 7 years ago

Using the secrets/mysql.json file, I've got the error "Uncaught Error: EntityError.noDatabase. Use middleware to catch this error and provide a better response. Otherwise, a 500 error page will be returned in the production environment."

This is my configuration file:

{
    "host": "localhost",
    "user": "vapor",
    "password": "vapor",
    "database": "vapor",
    "port": "3306",
    "encoding": "utf8"
}

My MySQL server is running. This is the code I use in Main.swift:

let drop = Droplet()
try drop.addProvider(VaporMySQL.Provider.self)
.
.
.
drop.get("/mysql") { request in
    var users : [User] = []
    do {
        try users = User.all()
    }
    return try drop.view.make("mysql", [
        "title": "MySQL example",
//        "users": users.makeNode()
        ])
}

And my entity file:

final class User: Model {
    var id: Node?
    var name: String

    init(name: String) {
        self.name = name
    }

    init(node: Node, in context: Context) throws {
        id = try node.extract("id")
        name = try node.extract("name")
    }

    func makeNode(context: Context) throws -> Node {
        return try Node(node: [
            "id": id,
            "name": name
            ])
    }

    static func prepare(_ database: Database) throws {
        try database.create("user") { users in
            users.id()
            users.string("name")
        }
    }

    static func revert(_ database: Database) throws {
        try database.delete("user")
    }
}
benjdum59 commented 7 years ago

It seems that I read The config file but Database (through provider) is not initialized when I use it in Entity class

dominik-hadl commented 7 years ago

Have you actually created the database with name vapor in your mysql server? And the user vapor with password vapor? I had the same setup and it worked for me.

benjdum59 commented 7 years ago

Yes: ~/Documents/Development/  mysql -u vapor -pvapor mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 13 Server version: 5.7.16 Homebrew

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | vapor | +--------------------+ 2 rows in set (0.00 sec)

mysql> use vapor Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A

Database changed mysql>

dominik-hadl commented 7 years ago

Which Vapor and MySQL provider version are you using? I've just tried with the exact same setup as you have and it seems to work for me.

benjdum59 commented 7 years ago

This is my Package manager configuration file:

import PackageDescription

let package = Package(
    name: "HelloWorld",
    dependencies: [
        .Package(url: "https://github.com/vapor/vapor.git", majorVersion: 1, minor: 1),
        .Package(url: "https://github.com/vapor/mysql-provider.git", majorVersion: 1, minor: 0),
        .Package(url: "https://github.com/vapor/fluent.git", majorVersion: 1, minor: 1)

    ],
    exclude: [
        "Config",
        "Database",
        "Localization",
        "Public",
        "Resources",
        "Tests",
    ]
)
dominik-hadl commented 7 years ago

I've just created a project with the same setup and everything works as expected. Could you possibly share your project?

benjdum59 commented 7 years ago

Of course. Thanks for helping: https://github.com/benjdum59/vapor-tuto

dominik-hadl commented 7 years ago

So, I've done these steps:

  1. Clone the repo
  2. Run vapor xcode --mysql in the root of the repo in terminal
  3. Run the app from Xcode

And everything worked fine and as expected.

I think that it looks like something is wrong with your MySQL setup. Could you try using an app like Sequel or something similar and connect to the the database that's running locally and see if that succeeds? Maybe if you could also do a clean clone of the repo and same steps as I did, just to see if it still doesn't work that might help.

benjdum59 commented 7 years ago

I'm still having the issue. Using sequel, I connected to the database with the configuration used inside project.

tanner0101 commented 7 years ago

Closing this for now due to inactivity. Feel free to re-open if it's unsolved.