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

SQL Server: Inserting Empty String in DateTime Column Becomes 1/1/1900 #279

Open
homestar9 opened this issue Apr 8, 2024 · 2 comments
Open

Comments

@homestar9
Copy link
Contributor

Imagine you want to save a struct like this to the database:

myFruit = {
	"name": "Banana",
	"color": "Yellow",
	"eatenDate": ""
};

qb.from( "fruit" ).insert( myFruit );

One would think that when you query the fruit you just inserted, the eatenDate value would be null. However, in MSSQL server if you insert an empty string (''), it will set the date to 1/1/1900.

Now, you could fix this by explicitly setting myFruit.eatenDate to { value="", null=true }. However, this extra step should be optional since most developers would expect QB to interpret an empty string as null when persisting the data to the database.

@elpete
Copy link
Collaborator

elpete commented Apr 9, 2024

This is tricky since qb has no knowledge that eatenDate is actually a date. And it is possible that you do want to insert an empty string into a column. I agree that it is unlikely, but I also have current instances where it actually inserts empty strings into columns.

Quick solves this by allowing you to specify what value is actually null. That defaults to an empty string, but can be overridden to be some other value if you need to insert empty strings. https://quick.ortusbooks.com/guide/getting-started/defining-an-entity#null-values

Could qb do the same? Possibly. It could be a query default that it checks. In any case it would be a breaking change.

@homestar9
Copy link
Contributor Author

Thanks for getting back to me, @elpete.

I also like the way Quick handles empty strings/nulls.

I ran into this issue with QB with my integration tests where I quickly need to insert data into a database to run through my test specs. Perhaps we could avoid creating a breaking change by adding a new configuration parameter in QB called treatEmptyStringsAsNull, which could default to false for backward compatibility.

If treatEmptyStringsAsNull is true, the query param parser could loop through all of the passed params and look for empty strings. If it finds one, it would change the resulting struct from { value=value, cfsqltype=type } to { value=value, cfsqltype=type, null=true }

Of course, developers could work around this limitation, and I have done so in the past by always passing the full struct value representation rather than just sending simple values. However, a change like this could make QB even simpler to work with and make a developer's code that much cleaner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants