Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 #3152

Closed
rnrajput opened this issue Feb 10, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@rnrajput
Copy link

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

Describe the bug

A clear and concise description of what the bug is.

To Reproduce

Steps to reproduce the behavior:

  1. Add package with configuration '...'
  2. Send request with options '...'
  3. See error

Expected behavior

A clear and concise description of what you expected to happen.

Environment

  • Vapor Framework version:
  • Vapor Toolbox version:
  • OS version:

Additional context

Add any other context about the problem here.

@rnrajput rnrajput added the bug Something isn't working label Feb 10, 2024
@mkll
Copy link
Sponsor

mkll commented Feb 11, 2024

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

@rnrajput
Copy link
Author

rnrajput commented Feb 11, 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 } }

@mkll
Copy link
Sponsor

mkll commented Feb 11, 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

@rnrajput
Copy link
Author

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\"}}" }

@mkll
Copy link
Sponsor

mkll commented Feb 11, 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.

@rnrajput
Copy link
Author

@mkll thanks now is working in mysql changes as LONGTEXT

@0xTim 0xTim closed this as completed Feb 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants