config.preparations.append(modelName.self) 中的错误使用未解析的标识符 'modelName'

Error in config.preparations.append(modelName.self) Use of unresolved identifier 'modelName'

我已经通过流利的提供者的正确方法为我的 模型 (Swift) 使用 vapor (server side)PostgreSQL provider(database),我按照fluent的一般方法,但是不知道哪里做错了,在extension for modelName的准备上, 在我的 modelName.swiftmain.swift.

的代码下方
 import Foundation
 import Vapor
import FluentProvider
import PostgreSQLProvider


final class modelName: Model {


    let storage = Storage()
    var id: Node?
    var name:String
    var displayName:String
       public var content: String

    init(content: String, displayName:String, name:String) {

        self.content = content
        self.displayName = displayName
        self.name = name
    }

    func forDataBase() {


        let array:[berichtDataPoint] = [intDataPoint(), boolDataPoint(), doubleDataPoint()]

        let _ = array[0] as! intDataPoint
        let _ = array[1] as! doubleDataPoint


        for point in array {

            switch point {
            case is  intDataPoint:
                print("int")
            case is doubleDataPoint:
                print("double")
            case is boolDataPoint:
                print("bool")
            default:
                print("error")
            }
        }

    }

  func makeRow() throws -> Row {
        var row = Row()
        try row.set("id", idKey)
        try row.set("displayName", displayName)
        try row.set("name", name)
        return row
    }

    init(row: Row) throws {
        content = try row.get("content")
        displayName = try row.get("displayName")
        name = try row.get("name")
    }

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


extension modelName: Preparation {
    static func prepare(_ database: Database) throws {
        try database.create(self) { modelName in
            modelName.id()
            modelName.string("displayName")
            modelName.string("name")

        }
    }

    static func revert(_ database: Database) throws {
        try database.delete(self)
    }
}

main.swift

import App
import Vapor
import FluentProvider
import PostgreSQLProvider

/// We have isolated all of our App's logic into
/// the App module because it makes our app
/// more testable.
///
/// In general, the executable portion of our App
/// shouldn't include much more code than is presented
/// here.
///
/// We simply initialize our Droplet, optionally
/// passing in values if necessary
/// Then, we pass it to our App's setup function
/// this should setup all the routes and special
/// features of our app
///
/// .run() runs the Droplet's commands, 
/// if no command is given, it will default to "serve"
let config = try Config()
config.preparations.append(modelName.self) \error is here '(Use of 
unresolved identifier 'modelName')

let drop = try Droplet(config)
try drop.setup()

try drop.run()

我认为根本原因是模块是分开的。 如果您将 vapor 项目创建为 vapor newmain.swiftRun 模块中,modelName.swiftApp 模块中。

// Package.swift

let package = Package(
    name: "hello",
    targets: [
        Target(name: "App"),
        Target(name: "Run", dependencies: ["App"]),
    ],

当访问其他模块class时,目标class的访问级别必须使用openpublic

// modelName.swift

public class moduleName: Model {
...

请注意,您还必须根据此更改修改其他方法声明。

谢谢。

将 modelName.swift 移动到 运行 文件夹