顶级错误:Fluent.EntityError.noDatabase

时间:2016-12-08 17:58:46

标签: postgresql vapor vapor-1

我正在尝试修复我最近在运行Vapor项目时遇到的错误。

它构建良好,但当它运行时,它崩溃。这是我的日志:

fatal error: Error raised at top level: Fluent.EntityError.noDatabase: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-800.0.58.6/src/swift/stdlib/public/core/ErrorType.swift, line 184
Current stack trace:
0    libswiftCore.dylib                 0x0000000100fe7cc0 swift_reportError + 132
1    libswiftCore.dylib                 0x0000000101004f50 _swift_stdlib_reportFatalErrorInFile + 112
2    libswiftCore.dylib                 0x0000000100fb3370 partial apply for (_assertionFailed(StaticString, String, StaticString, UInt, flags : UInt32) -> Never).(closure #1).(closure #1).(closure #1) + 99
3    libswiftCore.dylib                 0x0000000100dfb0a0 specialized specialized StaticString.withUTF8Buffer<A> ((UnsafeBufferPointer<UInt8>) -> A) -> A + 355
4    libswiftCore.dylib                 0x0000000100fb32b0 partial apply for (_assertionFailed(StaticString, StaticString, StaticString, UInt, flags : UInt32) -> Never).(closure #1).(closure #1) + 144
5    libswiftCore.dylib                 0x0000000100dfb5b0 specialized specialized String._withUnsafeBufferPointerToUTF8<A> ((UnsafeBufferPointer<UInt8>) throws -> A) throws -> A + 124
6    libswiftCore.dylib                 0x0000000100f57af0 partial apply for (_assertionFailed(StaticString, String, StaticString, UInt, flags : UInt32) -> Never).(closure #1) + 185
7    libswiftCore.dylib                 0x0000000100dfb0a0 specialized specialized StaticString.withUTF8Buffer<A> ((UnsafeBufferPointer<UInt8>) -> A) -> A + 355
8    libswiftCore.dylib                 0x0000000100dfae80 _assertionFailed(StaticString, String, StaticString, UInt, flags : UInt32) -> Never + 144
9    libswiftCore.dylib                 0x0000000100e1e540 swift_unexpectedError_merged + 569
10   App                                0x0000000100001ef0 main + 2798
11   libdyld.dylib                      0x00007fff974375ac start + 1
Program ended with exit code: 9

我正在使用VaporPostgreSQL包。这是我的Package.swift

import PackageDescription

let package = Package(
    name: "mist",
    dependencies: [
        .Package(url: "https://github.com/vapor/vapor.git", majorVersion: 1, minor: 2),
        .Package(url: "https://github.com/vapor/postgresql-provider.git", majorVersion: 1, minor: 1)
    ],
    exclude: [
        "Config",
        "Database",
        "Localization",
        "Public",
        "Resources",
        "Tests",
    ]
)

main.swift

import Vapor
import VaporPostgreSQL
import Auth
import HTTP

let drop = Droplet()
let auth = AuthMiddleware(user: User.self)

try drop.addProvider(VaporPostgreSQL.Provider.self)
drop.preparations.append(Post.self)
drop.preparations.append(User.self)
drop.preparations.append(Site.self)
drop.middleware.append(auth)

let admin = AdminController()
var site = Site(name: "", theme: "")

if let retreivedSite = try Site.all().first {
    site = retreivedSite
} else {
    drop.get { req in
        return Response(redirect: "http://localhost:8080/login")
    }
}

drop.get { req in
    return try drop.view.make("Themes/VaporDark/index", [
        "posts": Node(node: JSON(Post.all().makeNode()))
    ])
}

admin.addRoutes(to: drop)

drop.resource("posts", PostController())

drop.run()

我的postgres版本是9.6.1

出于某种原因,VaporPostgreSQL不会更新,我认为这可能是问题的一部分。我尝试了vapor xcodevapor buildvapor clean,但我无法获得最新版本。

enter image description here

2 个答案:

答案 0 :(得分:1)

我认为问题在于:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <configuration>
        <excludedGroups>com.bp.gis.util.HeavyTest
        </excludedGroups>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.18.1</version>
    <configuration>
        <includes>
            <include>**/*.java</include>
        </includes>
        <groups>com.bp.gis.util.HeavyTest</groups>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>integration-test</goal>
            </goals>
        </execution>
    </executions>
</plugin>

更具体地说,是if let retreivedSite = try Site.all().first { site = retreivedSite } else { drop.get { req in return Response(redirect: "http://localhost:8080/login") } } 电话。在调用Site.all()命令之前,我们不准备模型,因此,要在该点之前查找run(),需要手动准备模型。

希望这有帮助!

答案 1 :(得分:0)

:一种。

您应该验证您的秘密/ postgresql.json

中的信息
{
    "host": "127.0.0.1",
    "user": "username",
    "password": "",
    "database": "nameofyourdb",
    "port": 5432
}

确保配置信息正确无误。尝试从终端手动连接到postgresql实例,并确保您的数据库确实存在。

<强>乙

有时,数据会被破坏。尝试还原数据库实例并再次运行您的应用程序。如果您使用Xcode来运行项目,可以从Edit scheme / Run / Arguments:

执行此操作

enter image description here

prepare --revert

请注意,控制台会询问您是否确定要执行操作,您需要输入Y作为接受。之后你将需要删除上面的参数,下次运行你的应用程序时它将有一个新的数据库。

警告:此操作会删除您的所有信息,因此您显然应该在开发环境中或至少备份您的信息。