Is it possible to include Core Data entity type in constraint?











up vote
3
down vote

favorite












I'm working with Core Data + Swift 4.2 + Xcode 10. In my data model, I have an abstract entity A which has no parent entity, entity B which has A as its parent, and C which has A as its parent.



Entity A has a timestamp field, which is therefore inherited by B and C. I would like to impose a constraint that timestamp must be unique within an entity type. That is, I want all B items to have unique timestamps, and all C items to have unique timestamps, but some B item might have the same timestamp as some C item.



Is there a way to express that constraint in Xcode? The "Constraints" field in the entity editor wants a list of attributes. Timestamp is an attribute, so that's OK, but the entity type (B or C) is not. So I don't see a way to include entity type.



Is it possible that entity type is an implicit attribute? Just a shot in the dark here.



EDIT: To be clear, the reason I'm asking is that I tried to save an instance of B with timestamp T1 and an instance of C with timestamp T1 also, and I got an error to the effect that the constraint was violated. I was hoping that both instances would be saved (perhaps that was wishful thinking on my part). I am working with the Sqlite backend if that makes a difference.










share|improve this question
























  • Without knowing specifically what your code looks like, I can only speak in "physics for poets" terms. That said, you may consider creating a method wherever your entities are created to test for "uniqueness" before creating your entity.
    – Adrian
    Nov 10 at 1:34






  • 1




    Thanks for your response. I am hoping to assign the same timestamp to both objects, since they are created at the same time. I am working around the limitation by making one timestamp slightly different than the other, but that is unsatisfying; I would rather directly express the notion that the effective constraint is on the combination (entity type, constraint).
    – Robert Dodier
    Nov 10 at 2:20










  • Apple makes this warning when using Sqlite: "Be careful with entity inheritance when working with SQLite persistent stores. All entities that inherit from another entity exist within the same table in SQLite. "
    – Mike Taverne
    Nov 10 at 2:55










  • Understood, however, I don't think one can conclude that it's therefore impossible. In fact, I see that Core Data has constructed the table corresponding to A with a field Z_ENT which varies according to subentity. On the face of it, it seems a straightforward implementation of the desired goal would be to include Z_ENT in the constraint declared for the table.
    – Robert Dodier
    Nov 10 at 19:58















up vote
3
down vote

favorite












I'm working with Core Data + Swift 4.2 + Xcode 10. In my data model, I have an abstract entity A which has no parent entity, entity B which has A as its parent, and C which has A as its parent.



Entity A has a timestamp field, which is therefore inherited by B and C. I would like to impose a constraint that timestamp must be unique within an entity type. That is, I want all B items to have unique timestamps, and all C items to have unique timestamps, but some B item might have the same timestamp as some C item.



Is there a way to express that constraint in Xcode? The "Constraints" field in the entity editor wants a list of attributes. Timestamp is an attribute, so that's OK, but the entity type (B or C) is not. So I don't see a way to include entity type.



Is it possible that entity type is an implicit attribute? Just a shot in the dark here.



EDIT: To be clear, the reason I'm asking is that I tried to save an instance of B with timestamp T1 and an instance of C with timestamp T1 also, and I got an error to the effect that the constraint was violated. I was hoping that both instances would be saved (perhaps that was wishful thinking on my part). I am working with the Sqlite backend if that makes a difference.










share|improve this question
























  • Without knowing specifically what your code looks like, I can only speak in "physics for poets" terms. That said, you may consider creating a method wherever your entities are created to test for "uniqueness" before creating your entity.
    – Adrian
    Nov 10 at 1:34






  • 1




    Thanks for your response. I am hoping to assign the same timestamp to both objects, since they are created at the same time. I am working around the limitation by making one timestamp slightly different than the other, but that is unsatisfying; I would rather directly express the notion that the effective constraint is on the combination (entity type, constraint).
    – Robert Dodier
    Nov 10 at 2:20










  • Apple makes this warning when using Sqlite: "Be careful with entity inheritance when working with SQLite persistent stores. All entities that inherit from another entity exist within the same table in SQLite. "
    – Mike Taverne
    Nov 10 at 2:55










  • Understood, however, I don't think one can conclude that it's therefore impossible. In fact, I see that Core Data has constructed the table corresponding to A with a field Z_ENT which varies according to subentity. On the face of it, it seems a straightforward implementation of the desired goal would be to include Z_ENT in the constraint declared for the table.
    – Robert Dodier
    Nov 10 at 19:58













up vote
3
down vote

favorite









up vote
3
down vote

favorite











I'm working with Core Data + Swift 4.2 + Xcode 10. In my data model, I have an abstract entity A which has no parent entity, entity B which has A as its parent, and C which has A as its parent.



Entity A has a timestamp field, which is therefore inherited by B and C. I would like to impose a constraint that timestamp must be unique within an entity type. That is, I want all B items to have unique timestamps, and all C items to have unique timestamps, but some B item might have the same timestamp as some C item.



Is there a way to express that constraint in Xcode? The "Constraints" field in the entity editor wants a list of attributes. Timestamp is an attribute, so that's OK, but the entity type (B or C) is not. So I don't see a way to include entity type.



Is it possible that entity type is an implicit attribute? Just a shot in the dark here.



EDIT: To be clear, the reason I'm asking is that I tried to save an instance of B with timestamp T1 and an instance of C with timestamp T1 also, and I got an error to the effect that the constraint was violated. I was hoping that both instances would be saved (perhaps that was wishful thinking on my part). I am working with the Sqlite backend if that makes a difference.










share|improve this question















I'm working with Core Data + Swift 4.2 + Xcode 10. In my data model, I have an abstract entity A which has no parent entity, entity B which has A as its parent, and C which has A as its parent.



Entity A has a timestamp field, which is therefore inherited by B and C. I would like to impose a constraint that timestamp must be unique within an entity type. That is, I want all B items to have unique timestamps, and all C items to have unique timestamps, but some B item might have the same timestamp as some C item.



Is there a way to express that constraint in Xcode? The "Constraints" field in the entity editor wants a list of attributes. Timestamp is an attribute, so that's OK, but the entity type (B or C) is not. So I don't see a way to include entity type.



Is it possible that entity type is an implicit attribute? Just a shot in the dark here.



EDIT: To be clear, the reason I'm asking is that I tried to save an instance of B with timestamp T1 and an instance of C with timestamp T1 also, and I got an error to the effect that the constraint was violated. I was hoping that both instances would be saved (perhaps that was wishful thinking on my part). I am working with the Sqlite backend if that makes a difference.







swift xcode core-data






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 11:59









Austin Conlon

36213




36213










asked Nov 10 at 1:20









Robert Dodier

10.7k11633




10.7k11633












  • Without knowing specifically what your code looks like, I can only speak in "physics for poets" terms. That said, you may consider creating a method wherever your entities are created to test for "uniqueness" before creating your entity.
    – Adrian
    Nov 10 at 1:34






  • 1




    Thanks for your response. I am hoping to assign the same timestamp to both objects, since they are created at the same time. I am working around the limitation by making one timestamp slightly different than the other, but that is unsatisfying; I would rather directly express the notion that the effective constraint is on the combination (entity type, constraint).
    – Robert Dodier
    Nov 10 at 2:20










  • Apple makes this warning when using Sqlite: "Be careful with entity inheritance when working with SQLite persistent stores. All entities that inherit from another entity exist within the same table in SQLite. "
    – Mike Taverne
    Nov 10 at 2:55










  • Understood, however, I don't think one can conclude that it's therefore impossible. In fact, I see that Core Data has constructed the table corresponding to A with a field Z_ENT which varies according to subentity. On the face of it, it seems a straightforward implementation of the desired goal would be to include Z_ENT in the constraint declared for the table.
    – Robert Dodier
    Nov 10 at 19:58


















  • Without knowing specifically what your code looks like, I can only speak in "physics for poets" terms. That said, you may consider creating a method wherever your entities are created to test for "uniqueness" before creating your entity.
    – Adrian
    Nov 10 at 1:34






  • 1




    Thanks for your response. I am hoping to assign the same timestamp to both objects, since they are created at the same time. I am working around the limitation by making one timestamp slightly different than the other, but that is unsatisfying; I would rather directly express the notion that the effective constraint is on the combination (entity type, constraint).
    – Robert Dodier
    Nov 10 at 2:20










  • Apple makes this warning when using Sqlite: "Be careful with entity inheritance when working with SQLite persistent stores. All entities that inherit from another entity exist within the same table in SQLite. "
    – Mike Taverne
    Nov 10 at 2:55










  • Understood, however, I don't think one can conclude that it's therefore impossible. In fact, I see that Core Data has constructed the table corresponding to A with a field Z_ENT which varies according to subentity. On the face of it, it seems a straightforward implementation of the desired goal would be to include Z_ENT in the constraint declared for the table.
    – Robert Dodier
    Nov 10 at 19:58
















Without knowing specifically what your code looks like, I can only speak in "physics for poets" terms. That said, you may consider creating a method wherever your entities are created to test for "uniqueness" before creating your entity.
– Adrian
Nov 10 at 1:34




Without knowing specifically what your code looks like, I can only speak in "physics for poets" terms. That said, you may consider creating a method wherever your entities are created to test for "uniqueness" before creating your entity.
– Adrian
Nov 10 at 1:34




1




1




Thanks for your response. I am hoping to assign the same timestamp to both objects, since they are created at the same time. I am working around the limitation by making one timestamp slightly different than the other, but that is unsatisfying; I would rather directly express the notion that the effective constraint is on the combination (entity type, constraint).
– Robert Dodier
Nov 10 at 2:20




Thanks for your response. I am hoping to assign the same timestamp to both objects, since they are created at the same time. I am working around the limitation by making one timestamp slightly different than the other, but that is unsatisfying; I would rather directly express the notion that the effective constraint is on the combination (entity type, constraint).
– Robert Dodier
Nov 10 at 2:20












Apple makes this warning when using Sqlite: "Be careful with entity inheritance when working with SQLite persistent stores. All entities that inherit from another entity exist within the same table in SQLite. "
– Mike Taverne
Nov 10 at 2:55




Apple makes this warning when using Sqlite: "Be careful with entity inheritance when working with SQLite persistent stores. All entities that inherit from another entity exist within the same table in SQLite. "
– Mike Taverne
Nov 10 at 2:55












Understood, however, I don't think one can conclude that it's therefore impossible. In fact, I see that Core Data has constructed the table corresponding to A with a field Z_ENT which varies according to subentity. On the face of it, it seems a straightforward implementation of the desired goal would be to include Z_ENT in the constraint declared for the table.
– Robert Dodier
Nov 10 at 19:58




Understood, however, I don't think one can conclude that it's therefore impossible. In fact, I see that Core Data has constructed the table corresponding to A with a field Z_ENT which varies according to subentity. On the face of it, it seems a straightforward implementation of the desired goal would be to include Z_ENT in the constraint declared for the table.
– Robert Dodier
Nov 10 at 19:58












1 Answer
1






active

oldest

votes

















up vote
3
down vote













I don't think you can specify this behavior automatically in Core Data. But you can achieve this by adding another property to Entity A, and then making a constraint on the combination of that property and timestamp.



In this example, I added subtype to Entity A, and specified a constraint of subtype,timestamp.



enter image description here



These are the entity classes:



class EntityA: NSManagedObject {
@NSManaged var timestamp: String
@NSManaged var subtype: String
}
class EntityB: EntityA { }
class EntityC: EntityA { }


You need to set the subtype correctly before saving the entity:



entity.subtype = "B"


or



entity.subtype = "C"


or more generically:



entity.subtype = entity.entity.name!


It's not beautiful, but it works.






share|improve this answer























  • Thanks Mike, it would be neater if Core Data would expose an automatically-generated subtype attribute, but I can see that this does solve the problem.
    – Robert Dodier
    Nov 10 at 20:00











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235212%2fis-it-possible-to-include-core-data-entity-type-in-constraint%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote













I don't think you can specify this behavior automatically in Core Data. But you can achieve this by adding another property to Entity A, and then making a constraint on the combination of that property and timestamp.



In this example, I added subtype to Entity A, and specified a constraint of subtype,timestamp.



enter image description here



These are the entity classes:



class EntityA: NSManagedObject {
@NSManaged var timestamp: String
@NSManaged var subtype: String
}
class EntityB: EntityA { }
class EntityC: EntityA { }


You need to set the subtype correctly before saving the entity:



entity.subtype = "B"


or



entity.subtype = "C"


or more generically:



entity.subtype = entity.entity.name!


It's not beautiful, but it works.






share|improve this answer























  • Thanks Mike, it would be neater if Core Data would expose an automatically-generated subtype attribute, but I can see that this does solve the problem.
    – Robert Dodier
    Nov 10 at 20:00















up vote
3
down vote













I don't think you can specify this behavior automatically in Core Data. But you can achieve this by adding another property to Entity A, and then making a constraint on the combination of that property and timestamp.



In this example, I added subtype to Entity A, and specified a constraint of subtype,timestamp.



enter image description here



These are the entity classes:



class EntityA: NSManagedObject {
@NSManaged var timestamp: String
@NSManaged var subtype: String
}
class EntityB: EntityA { }
class EntityC: EntityA { }


You need to set the subtype correctly before saving the entity:



entity.subtype = "B"


or



entity.subtype = "C"


or more generically:



entity.subtype = entity.entity.name!


It's not beautiful, but it works.






share|improve this answer























  • Thanks Mike, it would be neater if Core Data would expose an automatically-generated subtype attribute, but I can see that this does solve the problem.
    – Robert Dodier
    Nov 10 at 20:00













up vote
3
down vote










up vote
3
down vote









I don't think you can specify this behavior automatically in Core Data. But you can achieve this by adding another property to Entity A, and then making a constraint on the combination of that property and timestamp.



In this example, I added subtype to Entity A, and specified a constraint of subtype,timestamp.



enter image description here



These are the entity classes:



class EntityA: NSManagedObject {
@NSManaged var timestamp: String
@NSManaged var subtype: String
}
class EntityB: EntityA { }
class EntityC: EntityA { }


You need to set the subtype correctly before saving the entity:



entity.subtype = "B"


or



entity.subtype = "C"


or more generically:



entity.subtype = entity.entity.name!


It's not beautiful, but it works.






share|improve this answer














I don't think you can specify this behavior automatically in Core Data. But you can achieve this by adding another property to Entity A, and then making a constraint on the combination of that property and timestamp.



In this example, I added subtype to Entity A, and specified a constraint of subtype,timestamp.



enter image description here



These are the entity classes:



class EntityA: NSManagedObject {
@NSManaged var timestamp: String
@NSManaged var subtype: String
}
class EntityB: EntityA { }
class EntityC: EntityA { }


You need to set the subtype correctly before saving the entity:



entity.subtype = "B"


or



entity.subtype = "C"


or more generically:



entity.subtype = entity.entity.name!


It's not beautiful, but it works.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 4:35

























answered Nov 10 at 4:22









Mike Taverne

5,64322138




5,64322138












  • Thanks Mike, it would be neater if Core Data would expose an automatically-generated subtype attribute, but I can see that this does solve the problem.
    – Robert Dodier
    Nov 10 at 20:00


















  • Thanks Mike, it would be neater if Core Data would expose an automatically-generated subtype attribute, but I can see that this does solve the problem.
    – Robert Dodier
    Nov 10 at 20:00
















Thanks Mike, it would be neater if Core Data would expose an automatically-generated subtype attribute, but I can see that this does solve the problem.
– Robert Dodier
Nov 10 at 20:00




Thanks Mike, it would be neater if Core Data would expose an automatically-generated subtype attribute, but I can see that this does solve the problem.
– Robert Dodier
Nov 10 at 20:00


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235212%2fis-it-possible-to-include-core-data-entity-type-in-constraint%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)