Not sure if this is a question specific to Yii or not but I am hoping someone might be able to help me with this issue regardless as I can’t seem to find any references to the issue on Google or StackOverflow
I am building a database that is to accept values in the likes or Arabic script. I am by no means an expert on databases collation or character sets, but from researching the issues, I am using charset UTF8 and collation utf8_unicode_ci in my database. However, after creating and testing the table, I found out I can still enter NULL values into any fields that are varchar NULL NOT (integer NOT NULL is correctly enforcing the rule).
Below is an example of a table created with Yii migration: -
CREATE TABLE ref_language (
id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
language_id varchar(6) NOT NULL UNIQUE,
iso_639 varchar(3) NOT NULL,
iso_3166 varchar(2) NOT NULL,
name_native varchar(40) NOT NULL,
name_english varchar(40) NOT NULL,
ltr tinyint(1) NOT NULL DEFAULT '1',
active tinyint(1) NOT NULL DEFAULT '1'
)
ENGINE InnoDB CHARSET=utf8 COLLATE=utf8_unicode_ci
Does anyone have any advice on what is going on here and the best solution?
I restarted MySQL and revert and re-ran the migration but it was still the same. I then copied the my.ini file (keeping the original) and renamed the copy ‘my.cnf’. Restarted MySQL, reverted and re-ran the migration and the bug is still there.
I accept I am very possibly going to look incredibly stupid here…
Have I got this right, that on a database level you can set a field to NOT NULL and if the field is varchar and an empty string is entered (i.e. you go into a record and delete the string in the varchar field) that this will be accepted as it’s technically an empty string as opposed to a NULL value?
However, when the a model is created with Gii, then it will find the properties for the field and if it’s set to NOT NULL, it will automatically filter it with the required validator, which will stop an empty string from being entered. Is that right? I have never been formally trained on databases and, if that scenario is the case, I have just misunderstood that technicality and I have probably always been testing out varchar/string NOT NULL behaviour through the Yii frontend.
Have I got this right, that on a database level you can set a field to NOT NULL and if the field is varchar and an empty string is entered (i.e. you go into a record and delete the string in the varchar field) that this will be accepted as it's technically an empty string as opposed to a NULL value?
Yes. Empty string is not a null value.
Validator would prevent both nulls and empty strings, that’s correct.