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

MySQL attempts to insert nulls for blank strings #680

Closed
andybellenie opened this issue Nov 25, 2016 · 7 comments
Closed

MySQL attempts to insert nulls for blank strings #680

andybellenie opened this issue Nov 25, 2016 · 7 comments

Comments

@andybellenie
Copy link
Contributor

Integer column, not null, default 0.

Try to create a new record, with that property as a blank string (e.g. an empty form field):

<cffunction name="new">
	<cfset company = model("Company").new()>
	<cfif StructKeyExists(params, "company")>
		<cfset company.setProperties(params.company)>
		<cfset company.testinteger = "">
		<cfset company.save()>
	</cfif>
</cffunction>

This will result in a MySQL crash: Column 'testinteger' cannot be null

Why include that property during insert if it is blank? The database default is essentially useless in this situation.

@perdjurner
Copy link
Contributor

I agree, there's no reason for Wheels to pass NULL (which is what a blank string translates to in the SQL) to a column that does not accept NULLs.

Maybe we can delete the object property when all of the below are true:

  1. it's a blank string.
  2. it's an insert.
  3. column does not allow nulls.
  4. column has a default value.

In theory we could do it any time the string is blank (like on an update for example) but that might be too much framework "magic" perhaps?

@andybellenie
Copy link
Contributor Author

This will still work without 3 and 4. Basically, we never try to set a blank string during insert.

If you try and set a blank string on a non-nullable column you won't get past validation.

The only scenario when you might want 3 and 4 is if you a) skip validation and b) really want your column to have an empty string in it.

As it is a Wheels principle that NULL and blank string are considered equivalent, I don't see it as required. What do you think?

@andybellenie
Copy link
Contributor Author

I agree about updates, this is only an issue because it stops database defaults from working when inserting

@perdjurner
Copy link
Contributor

Agreed, it'll probably be fine without 3 and 4 like you said.

@andybellenie
Copy link
Contributor Author

Ok, it's an easy fix - however it does break the test "Columns that are not null should allow for blank string during create"

...which sounds suspiciously like my way of writing...

@perdjurner
Copy link
Contributor

To play it safe (and to keep that test passing) maybe we only remove the object property when the column has a default value set then?

andybellenie added a commit to andybellenie/cfwheels that referenced this issue Nov 25, 2016
@perdjurner
Copy link
Contributor

@andybellenie

Can this be closed?

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