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.
swift xcode core-data
add a comment |
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.
swift xcode core-data
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
add a comment |
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.
swift xcode core-data
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
swift xcode core-data
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
add a comment |
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
add a comment |
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
.
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.
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
add a comment |
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
.
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.
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
add a comment |
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
.
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.
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
add a comment |
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
.
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.
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
.
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.
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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