-
Notifications
You must be signed in to change notification settings - Fork 53
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
Query works in pgAdmin, but returns System.InvalidCastException: Can't cast database type text to Int64 when executed using Npgsql #174
Comments
Update on this: I found that there was a type incompatibility in my database context. However, upon resolving this, I ended up having to split the execution of the query into 2 parts. A problem that I encountered here is that the query for |
Upon seeing this problem, I tried defining a function solely for the purpose of creating and dropping these tables, as seen below:
where
I hope this is clear enough. |
For the first issue, the result column types must match whatever data types you have for your entity properties. I don't really understand the second issue. What is the expected and actual outcome of your last posted code? |
Another thing, you need to escape user input here if you build your query manually, to avoid sql injections. |
For this, the API that I am currently developing is for internal use only, so I'm putting that further down the checklist first |
This one I already solved, because I overlooked the fact that the
The problem now lies in the fact that I cannot execute the CREATE TABLE and DROP TABLE queries along with my other queries. I don't know why. |
How do you execute your CREATE TABLE and DROP TABLE? Using EF6 or Npgsql only? What happens instead when you run the code? What is the result/status/return value, if any? |
Sorry, I'll get back to you tomorrow! |
How do you execute your CREATE TABLE and DROP TABLE? Using EF6 or Npgsql only? What happens instead when you run the code? What is the result/status/return value, if any? Hi, so for this, I use the the following string:
Where the string
As for the result, the query can never find the table, namely giving the error of To add to the confusion, this worked when all the queries were executed together. However, since now I have 2 different DbSets, I had to split the execution of the queries into 2 parts. This split was necessary because in the first half of the query it works with integers while the second half of the query works with the same table altered into a string. This is where the problem started. |
SUMMARY: I did a query in pgAdmin and it worked, but using the same query in C# using
FromSqlRaw
now returns a cast error.DETAILS:
I'm currently working on a C# RESTful API project that aims to use a string of arrays
requests
posted by a user and execute an SQL query tasked to find integers in a column of a mix of integers and text, add it up, and return asum
based on a key attached to the entry.This will return the text attached to the key as well as the summed up integer.
This already works using the following query:
But then there is an additional requirement where the sum value should be set to "NA", which results in an error because now the "sum" column of the results have to be set to TEXT. I came up with a workaround like such:
In the second chunk of code, you can see another table called
alteredtable
to transform the sum given intemptable
and store it as a string. However, performing this using Postman yields the following error:System.InvalidCastException: Can't cast database type text to Int64
Doing this query in pgAdmin works perfectly. What went wrong in executing this through Npgsql?
I've tried comparing the queries word for word and they seem to match.
To be honest, the whole point of adding
altertable
at all was to try and solve this casting problem, by casting everything intemptable
first and then copying everything over toaltertable
but as you can see, that solution also fell apart.Before adding this
altertable
element in the query, it already worked in pgAdmin even though the type of thesum
column in temptable is TEXT.I did some research online and this might be because of the weird execution order of an SQL query. However, if this is the case, shouldn't the query fail too when I ran it in pgAdmin?
I am so confused.
To give some more context, this is how the table looks like:
and posting
["Jean", "Mona", "Michael"]
should result inThe text was updated successfully, but these errors were encountered: