ActiveRecord: Invalid date when querying varchar - mysql

This gives me strange error.
> Customer.where(:my_text_field => "05007062").first
> Customer Load (0.9ms) SELECT `customers`.* FROM `customers` WHERE `customers`.`my_text_field` = '05007062' LIMIT 1
ActiveRecord::StatementInvalid: Mysql2::Error: Invalid date: 2012-00-00 00:00:00: SELECT `customers`.* FROM `customers` WHERE `customers`.`my_text_field` = '05007062' LIMIT 1
...
Does anyone know why could that be? I don't see any date in the query. Also, this happens only in production and not on development machine (both running linux).
I'm using Rails 3.1.0 and mysql2 0.3.10

Sounds like you may have some corrupted data in one of the records on your prod server. Do you have some other code that creates records in the db - including a date?

Related

MySql Query acts differently on my local version (8.0.21) and my staging (5.7.12)

I have an issue running a query on MySql using a Goapplication.
My local version works fine using mysql 8.0.21 but the same query on my staging version 5.7.12 on aurora fails
SELECT COUNT(*) AS cnt
FROM (
SELECT ? AS item_id, ? AS ar
)
AS A
JOIN item ON A.item_id = item.id
AND A.ar + item.existing_qty > item.qty;
Running this code in data grip with replacements works fine on both local and staging
Running this code but replacing the question marks with ints works fine
The error I get is:
Error 1054: Unknown column 'A.ar' in 'field list
I am thinking there is some driver / version issue
Does it work if you phrase the query like this?
select count(*)
from item
where item = ? and existing_qty + ? < qty
This is the same logic as your original query, and the parameters are passed in the same order.

Same query, different results

I'm running a cron daily, and keep records saved in a table. Wrote a query to see the daily progress of the users, but facing an issue.
I have local and production environments with separate data bases.
SELECT *, LAG(value) OVER(PARTITION BY node_id ORDER BY created_at) old_value
FROM ledger WHERE ledger_type_id = 1
This is the part of the query that is failing. I think the db driver has something to do it. The query was working on the prod data base, from my SQL client, but I just noticed that it is running MySQL 8, and the prod db is 10.3.16-MariaDB.
Tried the same query on my local data base - MySQL 5.7, and got this error:
[2019-08-06 16:12:47] [42000][1064] You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax
to use near '(PARTITION BY node_id ORDER BY created_at) AS old_value
After this I did 2 more tests:
Ran the query on the prod server it self, via phpmyadmin, and the server complete died, for a few minutes.
Ran the query from my code on the prod environment (laravel 5.8):
DB::select(DB::raw('
SELECT *, LAG(value) OVER(
PARTITION BY node_id ORDER BY created_at
) old_value
FROM ledger WHERE ledger_type_id = 1
'))
and got this in my laravel log:
production.ERROR: SQLSTATE[42000]: Syntax error or access violation:
1140 Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is
illegal if there is no GROUP BY clause
Am I doing something incorrect?

Error Number: 2013 Lost connection to MySQL server during query

I was testing my software which is in cpanel. But I am getting this whenever I run a single page. Other pages are working properly. the database table is having 12000+ data.is it because it takes much time to load
The same works perfect in localhost. Just now I uploaded it to cpanel.
Query
SELECT * FROM `service_sales_invoice` LEFT JOIN `service_sales_invoice_data` ON `service_sales_invoice`.`invoice_number` = `service_sales_invoice_data`.`invoice_number` WHERE `date` >= '2019-05-01' AND `date` <= '2019-06-20' ORDER BY `date`
Expected result is it works properly. But not.

Error Code: 2013. Lost connection to MySQL server during query with operator - OR

I know there is 100+ posts about it, but there is a information about time out etc, not about query...
MySQL Server: 5.7.17
MySQL Workbench: 6.3.8
Query:
SELECT transactionID, fromAccount,ToAccount, transactionDate, amount, type
FROM accounttransactions WHERE toAccount = '12345' #OR fromAccount = '12345'
Respond:
It works, but when I change my query and add(uncomment in this scenario) OR in WHERE statement then it Running for ~6s and looks like:
Error Code: 2013. Lost connection to MySQL server during query

Entity Framework 6 - DbFunctions.AddHours error

I've got a task to fetch a set of entities for a specific period of time.
I'm using code first approach and MySql server.
With the code
IQueryable<Entity> query = dbContext.Set<Entity>().AsNoTracking();
Entity[] entities = (from e in query
let last = (from ee in query orderby ee.Id descending select ee).FirstOrDefault()
where e.Timestamp >= DbFunctions.AddHours(last.Timestamp, -hours)
select e).ToArray();
And the following SQL generated by EF 6.1.3
SELECT
`Extent1`.`Id`,
`Extent1`.`Timestamp`
FROM `Entities` AS `Extent1` LEFT OUTER JOIN (SELECT
`Extent2`.`Id`,
`Extent2`.`Timestamp`
FROM `Entities` AS `Extent2`
ORDER BY
`Extent2`.`Id` DESC LIMIT 1) AS `Limit1` ON 1 = 1
WHERE `Extent1`.`Timestamp` >= (AddHours(`Limit1`.`Timestamp`, -(#p__linq__0)))
I face an error
Failed in 6 ms with error: FUNCTION mydb.AddHours does not exist
And indeed I haven't got that function at DB side.
Does anybody know how those DbFunctions work?
UPD
Similar questions
EntityFramework 6.1.3 and MySQL DbFunctions.TruncateTime does not exist?
how to use canonical functions in Entity Framework and Mysql
A workaround for now - just added an implementation of the function in DbInitializer
CREATE FUNCTION AddHours(timeValue datetime, addValue int) RETURNS datetime
RETURN DATE_ADD(timeValue, INTERVAL addValue HOUR);
You are trying to call a stored function of your database, but that does not exist. The solution is to properly create the AddHours db function. More information can be found here.