One-to-many different entity types - postgresql

I'm learning nest.js with postgresql and I'm stuck on the one subject. Let's suppose I have three entities: entity A, B and C.
I want one of the field from the entity C to reference either to A or B.
At this moment I could set two different columns, where one refers (as one to many) to entity A, and another column to entity B.
Is there any way I could sort it out having only one column for that?
Like
#One-To-Many(ref to A | B)

Related

How to deal with master tables

I have an ERD with a main table (A) which has one attribute(String) that is a FK to another table (B).
The issue that I have is that in B the only attribute is the PK; I just want to ensure that the user inputs only one of the allowed values in the main table attribute. I do not even want to update the B table from the application, as it will be a task so unusual that I'll do it directly in the DB.
I could treat B just as another Entity and deal with them with "regular" JPA, but I am a little troubled that maybe there are more efficient ways to do it*. All I want from B table is to get the full list of values and to ensure that the attribute value is correct.
So the question is: there is a specific pattern in JPA to deal with those master tables?
Thanks In advance.
*: My concern is creating / retrieving Entity B objects when all that it is needed is an string, every time an Entity A object is created retrieved.
I would simply use a native query to get all the strings from the B table, or map B as an entity to retrieve all the B Strings using a JPQL query, but not have any association from A to B.
The B string would be stored as basic String column in entity A. And if you try creating or updating an A instance with a string that is not in the B table, then you'll get an exception at flush or commit time because the foreign key constraint is broken.

Core Data: How to design a tree data structure from one core data entry

I am struggling with designing a coreData model where I have only one type of entry called "To-Do". Each To-Do entry has either 0, 1, 2, ... , or n relationships to other (sub) entries just like To-Do. So the relationships between the To-Do entries design a tree structure with an undefined number of child nodes. The following graphic should illustrate the case (E = core data entry):
E
/|\
/ | \
E E E
/ \
/ \
E E
/|\
E E E
My guess was to model that data like illustrated in the following graph. I didn't choose the inverse relationship because Xcode made a many-to-many relationship out of it which doesn't match the tree design.
Also I saw in the data model inspector something called "parent entry". So I started to believe I might have to create a second entry named "To-Do-Child" with the same attributes and make the other entry to the parent entry. The manual tells me that this might be the wrong path to go...
Questions:
How can I model this approach within the core data model file? Is one of the ones mentioned correct?
How will I be able to fetch all To-Do entries of a specified parent node? Since they arise from the same entry I have problems to address the exact To-Do subtree I want.
I think you need a relationship of parent (destination entity is your to do entity) which serves as the destination for the inverse relationship.
Entries at the top of the tree have nil value for this relationship.
For any to-do item, the set returned from the childToDos relationship will hold all the children. It doesn't matter that these are of the same class.

How do you determine subtype of an entity using Inheritance with Entity Framework 4?

I am just starting to use the Entity Framework 4 for the first time ever. So far I am liking it but I am a bit confused on how to correctly do inheritance.
I am doing a model-first approach, and I have my Person entity with two subtype entities, Employee and Client. EF is correctly using the table per type approach, however I can't seem to figure out how to determine what type of a Person a specific object is.
For example, if I do something like
var people = from p in entities.Person select p;
return people.ToList<Person>();
In my list that I form from this, all I care about is the Id field so i don't want to actually query all the subtype tables (this is a webpage list with links, so all I need is the name and the Id, all in the Persons table).
However, I want to form different lists using this one query, one for each type of person (so one list for Clients and another for Employees).
The issue is if I have a Person entity, I can't see any way to determine if that entity is a Client or an Employee without querying the Client or Employee tables directly. How can I easily determine the subtype of an entity without performing a bunch of additional database queries?
Use .OfType<Client>() in your query to get just the clients. See OfType.
e.g. entities.Person.OfType<Client>() ...
Use is to test if a Person object is a specific sub-class, e.g. if (p is Employee) ...
BTW why isn't it entities.People? Did you not select the pluralization option?

iPhone Development - Query related records using CoreData

I have a case where i have three entities with one-to-many and one-to-many relationships:
Entity A (Entity B relationhip),
Entity B (Entity A relationship, Entity C relationship),
Entity C (Entity B relationhip)
I have the reference of Entity A, and now i want to fetch all the related Entity C records. How can i do that? (with least amount of code)
Edit: Here's another way to put it.
Can we perform joins with CoreData. For example, (and this is a very crude example), We have a following entity-relationship:
Grand Parent (1)---(m) Parent
Parent (1)---(m) Child
So, now if i have "Albert" the Grand Parent, and i want to get all his grand children, how can i do that?
In case someone else stumble across a similar situation, here's what worked for me:
NSArray *allFieldValues = [myEntityA valueForKeyPath:#"Entity B relationship.Entity C relationship.requiredFieldInEntityC"];
I was mainly interesting in reading the data of a single field in Entity C (that's linked to myEntityA object). The key concept here is that "don't think of CoreData as a 'database'".

Map classes to database with ado.net

I want to know if there is way to create a database out of existing classes with the ado.net entity framework or to map existing classes to a database.
I looked for tutorials and only found ways to create the classes with the entity model designer.
As an example I have the class Bird with Properties Length and Age
On the database I have a table named Bird with columns Length and Age
Now I don't want the designer to create new classes out of the database. Instead I want to map the Class Bird directly to the table Bird. With Linq2Sql this was possible by creating the mapping manually. Is there a possibility in the ado.net entity framework?
With kind regards
Sebastian
What's the difference between mapping a class C onto a table T or mapping T onto a class C ?
O/R mapping isn't about mapping classes to tables, it's about defining an entity model and projecting it to tables AND classes simultaneously. After all, your classes and tables don't fall out of the sky: they're based on definitions you have, e.g. there has to be an entity customer, with fields A, B and C, and THEN you're defining the entity Customer with fields A, B and C which leads to a table Customer with fields A, B and C and a class Customer with fields / properties A, B and C and the mapping between them, because they represent the same entity