Skip to content

Issue with date values in queries #1243

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

Closed
aidanharan opened this issue Oct 10, 2024 · 2 comments · Fixed by #1246
Closed

Issue with date values in queries #1243

aidanharan opened this issue Oct 10, 2024 · 2 comments · Fixed by #1246

Comments

@aidanharan
Copy link
Contributor

aidanharan commented Oct 10, 2024

I am not sure if this is related, but we are still facing the same issue in Rails-7.2.1 and activerecord-sqlserver-adapter 7.2.1.
.where('date_column < ?', date) is throwing "undefined method `value_for_database'"

Originally posted by @aaronsajo in #1166 (comment)

I also came across this today.

where("written_on < ?", Time.now) ✅
where("written_on < ?", Date.today) ❌
where("written_on < ?", DateTime.current) ❌
So, it seems to work only for Time objects, but not Date or DateTime objects.

Adding checks for type.is_a?(Date) || type.is_a?(DateTime) in #basic_attribute_type? would fix these specific cases, but I don't know the codebase well enough to say, if this would break something else.

@nielsjansendk
Copy link

I have something that may or may not be related to this. I have the same problems with dates. When I do this:

MyModel.where(updated_at: (10.days.ago.to_date...Date.tomorrow)).to_sql

I get this answer:

SELECT [my_models].* FROM [my_models] WHERE [my_models].[updated_at] >= '10-05-2024' AND [my_models].[updated_at] < '10-16-2024'"

However, I do not use american date formats. And I don't believe I am setting that anywhere. So maybe the problems is that dates do not default to iso-format as they used to?

@andsip
Copy link
Contributor

andsip commented Oct 15, 2024

@nielsjansendk: I think, if you don't set the format explicitly anywhere in your codebase, the format is fetched from the SQL Server.
Maybe you can run DBCC USEROPTIONS on your SQL Server and post the value of option dateformat here?

Or, if you are using Azure, the dateformat is fetched via SELECT [dateformat] FROM [sys].[syslanguages] WHERE [langid] = @@LANGID


So maybe the problems is that dates do not default to iso-format as they used to?

Was this the case, that default was iso format? I don't see this anywhere in the last few versions of the adapter.

andsip added a commit to andsip/activerecord-sqlserver-adapter that referenced this issue Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants