Fluent: Adding parent and children at the same time

Hi all,

I've struggling with this already a day and so far I've not found any answers yet. I'd like to know if it is possible to add a parent model together with it's children at the same time. So far, I've only seen examples that create the model classes first and then create the link between them. What I've been trying to do is different though. In my database I have two model classes a Dataset and an Image. A Dataset can have multiple images and thus I've modelled a parent-child relationship between them, like so:

final class Dataset: Content, PostgreSQLModel
{
    var id: Int?
    var name: String
    var uuid: UUID

    init(name: String, uuid: UUID)
    {
        self.name = name
        self.uuid = uuid
    }
}

extension Dataset 
{
    var images: Children<Dataset, Image> 
    { 
        return children(\.datasetID)
    }
}

final class Image : Content, PostgreSQLModel
{
    var id: Int?
    var uuid: UUID?
    var filename: String
    var datasetID: Dataset.ID

    init(filename: String, datasetID: Dataset.ID)
    {
        self.filename = filename
        self.datasetID = datasetID
    }
}

extension Image 
{
    var dataset: Parent<Image, Dataset> 
    {
        return parent(\.datasetID)
    }
}

Note this code has been shorted for clarity. What I'm now trying to do is the following: I loop through a directory structure that contains a subdirectory for each dataset and each subdirectory contains the images for that dataset. I would like to add the dataset and images at the same time. I'm doing this (for now) in the boot.swift script:

public func boot(_ app: Application) throws {  
    var imagePath = FileManager.default.currentDirectoryPath
    imagePath.append("/Public/images")
    print("Scanning for datasets in: \(imagePath)")
    let datasets = Utils.findDatasets(path: URL(fileURLWithPath: imagePath))

    for dataset in datasets
    {
        print("Found new dataset \(dataset.name)")
     
        app.withPooledConnection(to: .psql) { conn in
            return conn.transaction(on: .psql) { conn in
                return dataset.create(on:conn)
                
                /*.flatMap { dataset in
                    let images = Utils.findImagesForDataset(dataset:dataset, path: URL(fileURLWithPath: imagePath))
                    return images[0].create(on: conn)
                }  */
                
            }                
                        
        }      
    }

}

However, I haven't been able to get it right. The code above is able to add a Database model to the database but if I add the commented out flatMap code, I get this error (I've tried to simplify things first by trying to add one image first before trying to add the whole array):

error: member 'psql' in 'DatabaseIdentifier<_>' produces result of type 'DatabaseIdentifier<PostgreSQLDatabase>', but context expects 'DatabaseIdentifier<_>'
        app.withPooledConnection(to: .psql) { conn in

Is there a way to get it working like this? Any help will greatly appreciated :smiley:

kind regards,

Tim