Coder Social home page Coder Social logo

Swift Vapor-Save records in Mysql JSON data type during mapping with swift get error- Could not convert MySQL data to String: <MYSQL_TYPE_JSON about vapor HOT 6 CLOSED

rnrajput avatar rnrajput commented on May 18, 2024
Swift Vapor-Save records in Mysql JSON data type during mapping with swift get error- Could not convert MySQL data to String: from vapor.

Comments (6)

rnrajput avatar rnrajput commented on May 18, 2024 1

@mkll thanks now is working in mysql changes as LONGTEXT

from vapor.

mkll avatar mkll commented on May 18, 2024

@rnrajput Sorry, but all the local telepaths are on vacation right now, dude.
¯\(ツ)

from vapor.

rnrajput avatar rnrajput commented on May 18, 2024
Screenshot 2024-02-11 at 9 44 33 AM func prepare(on database: Database) async throws { try await database.schema("master_Interface_tb") .id() .field("customer_id", .string, .required) .field("page_name", .string,.required) .field("page_json", .custom("JSONB"), .required) .create() } final class MasterInterface: Model, Content { static let schema = "master_Interface_tb" @id(key: .id) var id: UUID? @field(key:"customer_id") var customerId: String @field(key:"page_name") var pageName: String @field(key:"page_json") var pageJson: String? init() { } init(id: UUID? = nil, customerId: String, pageName: String, pageJson:String?) { self.id = id self.customerId = customerId self.pageName = pageName self.pageJson = pageJson } }

from vapor.

mkll avatar mkll commented on May 18, 2024

@rnrajput First, we format your code:

func prepare(on database: Database) async throws {
	try await database.schema("master_Interface_tb")
		.id()
		.field("customer_id", .string, .required)
		.field("page_name", .string,.required)
		.field("page_json", .custom("JSONB"), .required)
		.create()
}

final class MasterInterface: Model, Content {
	static let schema = "master_Interface_tb"
	
	@id(key: .id) var id: UUID?
	@field(key:"customer_id") var customerId: String
	@field(key:"page_name") var pageName: String
	@field(key:"page_json") var pageJson: String?
	
	init() { }
	
	init(id: UUID? = nil, customerId: String, pageName: String, pageJson:String?) {
		self.id = id
		self.customerId = customerId
		self.pageName = pageName
		self.pageJson = pageJson
	}
}

Next, let's look at the correspondence between the table schema and the corresponding model.

We can see that for page_json column name you have JSON definition in your schema and String definition in your model, and that the error message you receive matches your code exactly. It couldn't be any other way.

JSON definition in a schema is used when another nested model is stored in the corresponding model variable. In this case, the database driver automatically encodes and decodes the entire model hierarchy.

If you have a String property in your model, then in the schema the corresponding field should also be defined as String.

You should read the relevant basic sections of the documentation:
https://docs.vapor.codes/fluent/schema/#dictionary

from vapor.

rnrajput avatar rnrajput commented on May 18, 2024

page_json column name as a JSON string . it is not JSON, same things work on when i post data it works successful { "customerId":"AB02", "pageName": "LoginPage", "pageJson": "{\"loginPage\":{\"navigationTitle\":\"LoginPage\",\"navigationFontSize\":20,\"navigationTitleColor\":\"#000000\",\"alignment\":\"center\"}}" }

from vapor.

mkll avatar mkll commented on May 18, 2024

page_json column name as a JSON string . it is not JSON

The MySQL 8.0 documentation says the following:

"JSON documents stored in JSON columns are converted to an internal format that permits quick read access to document elements. When the server later must read a JSON value stored in this binary format, the value need not be parsed from a text representation. The binary format is structured to enable the server to look up subobjects or nested values directly by key or array index without reading all values before or after them in the document."

So, it's not a "JSON string", it's internal JSON representation and cannot be read as String. Exactly the same thing the error message says ("Could not convert MySQL data to String: <MYSQL_TYPE_JSON").

If you want to write JSON as a string, want to read it as a string and work with it as a string, then you should define a .string data type in the column rather than JSON. This is exactly how it works.

from vapor.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.