-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
[BUG]: VARCHAR(255) NOT NULL does not accept an empty string #16426
Comments
There is a way to avoid this validation in the manual, but I don't think you should use it because the impact is too big. How is this a realistic method? class Testing extends \Phalcon\Mvc\Model
{
public function beforeValidation()
{
if ($this->address=== '') {
$this->address= new \Phalcon\Db\RawValue('""');
}
}
} or class Testing extends \Phalcon\Mvc\Model
{
public function initialize()
{
$this->modelsMetaData->setEmptyStringAttributes($this, ['address'=>true]); // Correct mistakes made when copying and pasting
}
} I asked a similar question before, but no one seemed interested and I didn't get any replies. Null and "" should have different meanings, but both are treated as if isset emptyStringValues[field] {
if value === null {
let isNull = true;
}
} I think that the current problem can be avoided with these, but I am very doubtful whether this is a good implementation of Phalcon. |
@s-ohnishi You are right, disabling the null-validation is not a good option. I also agree that the line you reference to really is strange:
It looks like some trick to automatically treat empty values from the forms when you get In this case, a clean solution might be listing any column defined as
Additionally, I do not like the
This evaluates to true only if the default value is set and is not null. I.e., if the default value is null it evaluates to false. There is a better method |
@zacek I'm also glad to see that a correction is being made. |
Just checked it, introspections does not fill emptyStringValues, annotation strategy does. My issue is with the emptyStringValues vs defaultValues, trying to figure out the why. We do have a protected function in the model allowEmptyStringValues(), propably is supposed to be used in the initialize, but will not be cached. @zacek can you check if allowEmptyStringValues() works has expected while I make some checks and tests. |
OK so the default behaviour sinde 3.0 of the ORM is to assume emptyStrings as NULL, unless you set it to allow. If this is a treated like a bug then fix is rather simple. |
If this comment "Get string attributes that allow empty strings as defaults" is correct, then if you set an empty string in a field, the default value will be set instead, right? I think there are two problems. Inserting an empty string into a field that specifies NOT NULL is not a problem in the first place, so I think there was no need to verify it.
Perhaps this is a misunderstanding caused by the name "emptystring". |
@s-ohnishi the code is a bit confusing, I can try and simplify it. The issue is that this is a change in behaviour, while I disagree with it, it will break some applications and we do not want that, v6 will have this sorted out. Can you confirm that allowEmptyStringValues in initialize works and is somewhat a solution? |
@rudiservo However, if a default value is set for an existing record when the table is defined, that should be used by passing
Where is it checked? |
@s-ohnishi Don't worry about zephir, if you could test the setEmptyStringAttributes and check if it works has you expect would be great. For your second question.
So if it has default values, the continue will terminate the current key loop and move on to the next key. If the record exists, this come in effect.
so if I am reading this correctly, if the value is null on a non numeric field and the record exists, it is a 'PresenceOf' error. Can you confirm this in your testing? If this is unwanted behaviour or a bug, what would the expected behaviour be? |
Sorry, I was wrong. In strict mode, you will get However, I don't think this part is very accurate.
If you set an empty string in a column that is It seems like they are afraid that the behavior will change, but I think it would be a good idea to undo things that were originally possible but became exceptions. |
@s-ohnishi thanks for the feedback. Making these kind of changes is something you have to make argument for, the against argument is it might break someone's application, the issue is not the work around it's the default behaviour changes. You have to convince @niden , I would push for more changes in phalcon v5 but we are trying to push v6, so time is limited. |
We will need to leave this as is. Any change we make in the logic has the potential of breaking existing applications and that is something we do not want. Whatever change we make has to be communicated well in advance with the community so as upgrades can be a bit less painful. In v6 we can change the behavior to become more clearer and easier to handle but not v5 |
This will change the logic, but the effect is that setting an empty string to a column that is |
Questions? Forum: https://phalcon.io/forum or Discord: https://phalcon.io/discord
Describe the bug
Model with column declared as
varchar(255) NOT NULL
does not accept an empty string. It does not distinguishNULL
value and the empty string.To Reproduce
The result is
Expected behavior
Empty string should be accepted,
NULL
not.Details
Version => 5.2.1
Build Date => Mar 1 2023 13:57:18
Powered by Zephir => Version 0.17.0-$Id$
Additional context
Running in docker.
The text was updated successfully, but these errors were encountered: