How to make JSON nest Class or Structure

Hello. I will post to the forum for the first time.
I’m trying to make my own Vapor APP after finishing the book and video tutorial now.

I encountered some things I could not do in it. It is trying to create a class or struct json is nesting in JSON and save it in postgre.

In addition to suffering from errors such as the format of Future not matching, even if you define an empty array in Class and try to save it, it is an error such as different Element.

How do you manage a model that JSON has nested within JSON?
Please let me know if you do not mind.
I created MyAcronymModel and JSON format is

MyAcronym.swift
import Vapor
import Foundation
import FluentPostgreSQL

final class MyAcronym:Codable,Content,PostgreSQLModel,Parameter {
    var user:User.Public
    var acronyms:[Acronym]
    var id:Int?
}

extension MyAcronym:Migration {
    
}

MyAcronymController.swift
import Foundation
import Vapor
import FluentPostgreSQL
import Authentication

struct MyAcronymControler:RouteCollection {
    func boot(router: Router) throws {
        let myAcronymRoute = router.grouped("api/myacronym")
        let tokenAuthMiddleware = User.tokenAuthMiddleware()
        let guardAuthMiddleware = User.guardAuthMiddleware()
        let tokenAuthGroup = myAcronymRoute.grouped(tokenAuthMiddleware, guardAuthMiddleware)
        /*going to make createMyAcronymRoute
         */
    }
    
    func createMyAcronyms(_ req:Request,UserData:User.Public) -> Future<MyAcronym>{
          return  try req.parameters.next(User.self).map(to:[Acronym].self){ user in
            try user.acronyms.query(on: req).all()
            let data = myAcronymData(user: UserData, acronyms: queryData)
      }
    }
}

struct myAcronymData {
    var user:User.Public
    var acronyms:[Acronym]
}

Multiple errors will be issued at func createMyAcronyms. The rest is the same as the tutorial. How did anyone solve if someone had a similar challenge?

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

Ok so models with models inside…

The answer is it’s complicated! You can’t just set a property like you have, because it doesn’t work with the database.

There is a way to store models like that, but it means you can’t just query the Acronym table, since it doesn’t exist. If you want to return something like that in JSON, you need to create a new Content type and populate it manually. It’s known as the N+1 query and is something that Fluent doesn’t do very well.

If you join the Discord forum, there’s lots of solutions on there for it.