Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoreData - can't set empty string as default value for attribute

I have an entity in my datamodel with a string attribute that is currently optional, and I'd like to convert this to a required attribute with a default value of the empty string.

As others have discovered, leaving the default value blank in the Xcode CoreData data modeller results in validation errors (since the designer interprets this as NULL), but trying '', "", or @"" as the default value results in those literal characters being interpreted as the default, rather than the empty zero-length string, as desired.

I did find this thread on Google, however, apart from the solution being really ugly (model definition split between the .xcdatamodel and objc source), it also doesn't work for lightweight migrations because those migrations are done solely based on the .xcdatamodel files and the objc logic from your entity implementations isn't loaded.

Is there any way to achieve this in the data model designer?

like image 512
glenc Avatar asked Mar 01 '11 12:03

glenc


People also ask

What is the use of null attribute in EF Core?

The attribute doesn't help you in data validation. It even causes you trouble because it restricts your querying options. First, contrary to EF6, EF core doesn't do any data validation. So your good intention to block null strings from entering a database field that is nullable can't be fulfilled by any attribute.

What is the default value of allowemptystrings in dynamic data?

The default value is false. When you set AllowEmptyStrings to true for a data field, Dynamic Data does not perform validation and transforms the empty string to a null value. This value is then passed to the database. If the database does not allow null values, it raises an error.

Why can't I query for NULL values with string names?

That assumption is what causes you trouble. The query will never return records with null names not even when explicitly querying them. With string name = null; no records with null names will be returned. But now for the total bummer, EF won't even allow you to query for null values explicitly by an additional condition:

Does the attribute attribute help in data validation in EF6?

The attribute doesn't help you in data validation. It even causes you trouble because it restricts your querying options. First, contrary to EF6, EF core doesn't do any data validation.


1 Answers

This is a very interesting question. After some testing I don't think this is possible because of the way the text field in the data model is configured.

In principle, you could use the unicode empty-set character of \u2205 to represent a default empty string but the text field does not seem to accept any escapes so it converts any attempt to escape a unicode character code to the literal string of the code characters themselves e.g. entering '\u2205' ends up as the literal text '\u2205'.

In theory you could write a utility app to read in the graphically generated managed object model file and then programmatically set the attribute default to equal an empty string and then save the file back to disk. I say "in theory" because there is no documented way to way to save a managed object model file from code. You can read one and modify it in memory but not persist the changes.

Bit of an oversight, I think.

I don't think you have any choice but to set the default empty string pragmatically when the model first loads. That is simple to do but it's ugly and you'll have to remember you did (especially if you migrate versions) but I think right now that is the only choice.

like image 59
TechZen Avatar answered Sep 21 '22 01:09

TechZen