TypeORM Unidirectional Many To One relation - typeorm

I am creating the entities that make up my db schema.
I have the entity Photo that is bound to the entity Hashtag in a many to one relation (each photo has one hashtag, and each hashtag can belong to many photos).
In order to do this, as per TypeOrm documentation, I have to do the following.
In the Photo entity :
#ManyToOne(type => HashtagEntity, hashtag => hashtag.Photos)
#JoinColumn({ name: 'HashtagId' })
Hashtag: HashtagEntity;
And in the Hashtag entity :
#OneToMany(type => PhotoEntity, photo => photo.Hashtag)
Photos: PhotoEntity[];
I have read that all entities are eagerly loaded in TypeOrm. Does this mean that each time I query a hashtag I materialize all the photos associated to it?
If this is the case, it's very bad as I only need the reference to the Hashtag from the photo and not the other way around.
How can I solve this? Should I use a OneToOne relation on the Photo entity or is there a way to make a unidirectional ManyToOne?

TypeORM relations are not automatically eagerly loaded unless you specify otherwise in the the Entity definition.
In order to materialize relations, the name of the foreign key property must be specified in the relations array of the query.

Related

How to save array to an attribute of an entity?

I have a question about core data. I have an Entity named User and this entity has 3 attributes: name, images, videos. How can I save an array of multiple images or videos to the attributes images or videos?
With relational databases, whenever you have one attribute that is going to have a bunch of values for a single entity, you have a one to many relationship and you actually need another entity. So in a strict relational database, you would create a new entity, Image, that had one column pointing to the image's data and a second column pointing to the user that that image is associated with, a foreign key. In CoreData, they represent these foreign key columns as a relationship. So you'll have to make a new entity for each 1-to-M(any) relationship you have and give them a relationship back to user, and in turn user to them.
Try this tutorial, it may help you.

how to inset a new object in CoreDta and reflect the change in the relationship between two entities?

in my IOS10, swift 3, xcode8 application, I have Movie in coreData.
Movie: {id:Int32, name:String, genre: [ {Genre} ] }
Genre: {id:Int32, name:String}
the way I did it, is by creating a movie entity, and a genre entity, and set up many-to-many relationship between them...
is this correct ? cause one movie can have many genres, like action, comedy
and a genre, like romantic, can be associated with many movies.
my second question is, when I am saving a new movie to CoreData, How will the Genre entity know what happened, is there like a CoreData Feature that will do that automatically? or I should add a movie to the genre Set, and add a genre set for movie ?
my end goal is to be able to search for movies by titles, or by genre.
This is fairly basic, so I would recommend researching it: CoreData Documentation
But a basic rundown is when you create a many<->many relationship CoreData creates a connections table between the two models. The way coreData works is with managed objects, meaning every time you try and access an attribute of an NSManagedObject it will be the value in the saved database.
When you add something to a many-many relationship, the connections table will create the connection, and when access it from either model, the results will be based on that connections table.
TLDR, in CoreData if you have a relationship between two models, you only need to add the connection to one of the models.

Creation of relationship with attribute in CoreData with iOS in Objective C

I would like to create a model with two entities Orders and Products. They are linked by a relationship 'Contain' that has an attribute 'quantity'. How can I represent that in CoreData ? (Do not send me the ray tutorial, or any tutorial on youtube, I think I have done every thing). It is very important the relationship with attribute and not something general. (I know that it is not a database, but it is a Conceptual data model/Conceptual Schema as it is named by "entities" and "relationship" so if there is relationships, there must be a way to have relationships with attribute).
EDIT :
Am I doing the right thing by not adding id_order and id_product to the Contain entity ?
In CoreData, Contain would be another entity. It would have relationships to Orders and Products and a quantity attribute.
You cannot add attributes to a relationship in CoreData.

Core Data Programming Guide: intermediate join entity example, MUTUAL or NOT?

In the example of using "intermediate join entity"
To find out who one person’s friends are, you have to aggregate all the friend destinations of the friends relationship, for example:
NSSet *personsFriends = [aPerson valueForKeyPath:#"friends.friend"];
Is above line of code getting a given person's MUTUAL friends? or just ONE-WAY friends, which means only getting "peoples who are treated by this given person as his friends"?
I am not certain, because "To find out who one person’s friends are" sounds like ONE-WAY friendship (that could be why there is a strange relationship befriendedBy represents those who count the source as their friend. FriendInfo represents information about one friendship, “in one direction.” .)
This is a really confusing example. There are two possibilities.
In most cases being "friends" is a mutual thing. In this case you would have a self-referencing many-to-many relationship of a Person, perhaps called friends. The relationship would be mutual.
You seem to be implying that it is possible to add another person as a friend even if that person is does not reciprocate. In order to lift the confusion give this many-to-many relationship another distinct name, e.g. contacts. This would be the Persons that have been added unilaterally. In Core Data, all relationships are best modelled as mutual, so you can use another relationship potentialFriends that is the inverse relationship of contacts. Maybe there are better names, but you get the idea.
The intermediate join entity is only necessary if you want to store additional attribute with a particular relationship, e.g. the date a contact request was made. In this case, you would have the join entity e.g. friendLink, which would have a to-one relationship to two distinct Persons. You can model the state of the link (unilateral or mutual) in this entity.

Core Data - Relations queries

In core data,
I want to use relationship. But I have a doubt in my mind. Apologies but I didn't find clear answers on various websites and blogs. Other things like Add, Fetch, Delete queries are clear to me. But I have some questions in core data which are still not clear.
I have made two Entities:
Photographer [Attributes : name and camera]
Photo [Attributes: zoner and photographerName] .
1) I have connected them with relations. So if I connect this two entities with relations then I should remove that photographerName from Photo?
2) As I have connected these two entities with the relation then how can I use the photographer name with the photo Entity?
3) How can I add values in this if I use it with relations? [Now it is showing me Null in the relation from sqlite browser]
So if I connect this two entities with relations then I should remove that PhotographerName from Photo ?
Generally, yes, it is redundant.
how can I use the photographer name with the photo Entity ?
self.photographer.name
(assuming you are in the Photo class and the relationship names is photographer)
How can I add values in this if I use it with relations
I guess you mean how can I set the relationship value. Create an instance (or find an existing instance) or each entity and then:
photo.photographer = photographer;
Notes:
Ensure that the relationship has an inverse
Names the relationship ends photographer and photos (1 to many)
Try to set the photographer of a photo, or use the relationship methods auto-generated in the Photographer class to add photos

Resources