Creating a data object relationship system - database

I would like to store an object that has a name and some attribute and any kind of database and have another that that document relationship between between them
Let say this
Object A
Object B
Object C
Object D
A is related to B
A is related to D
B is related to C
All relation have also attribute that give the relation order (uni (from,to) or (to,from) or bidrectional).
Then, I would like to do some query that will show me A is related to C by passing in B. A kind of path between 2 objects.
Kind of graph database like Neo4J
Thanks for your input

Question is rather old but anyways:
The Neo4j documentation actually has an example that is very close to answering the question.
http://docs.neo4j.org/chunked/stable/graph-algo-path-finding.html

Related

Multiple objects with same FK in context.entity.local [Entity Framework]

I have some problems regarding entity framework and saving to the db.
As my current program works, it deserializes a json-object which results in a list with objects that matches the database. Each of these objectst looks like this:
Every object is a parent with a relation to one child-object.
Each child object have a relation to one or many parent-objects.
After the deserialization is complete each child-objects is created as new object for every parent (meaning I get several instances of the same object).
When i try to save the objects to the db, this offcourse doesn't work since I'm trying to insert many child-objects whith the same pk. I can clearly see that context.childentity.local contains many objects with the same pk.
Is there any easy way to solve this issue? Can I in some way tell EF to refer all duplicates to the same object?
Best regards Anton

Relating one model to multiple models based on field

Here's a brief description of the models:
Model A represents a piece of equipment and has a name and a state
Model B represents a specific state of many model A's
Model B should relate to many model A's but only for a specific state of model A
Is it possible to model something like this in the datastore? I need the state of model A to be independent of any model B's, but when I peer into a model B I need to know what the state of the model A's should be that the model B is representing.
The current way I am achieving this is by making model B have string fields representing the different model A's with the name of the field being the name of the model A and the value of the field being the state the model A is supposed to be in.
This works, however it's completely static and requires manually adding fields into model B when the number of model A's change. I'm looking for a dynamic approach to solve this problem.
I hope this isn't too confusing, please ask for more clarification if it's needed.
You can use reference properties for this:
class A(db.Model):
state = db.ReferenceProperty(collection_name="equipment")
When you create A, you set the state property to the corresponding B entity.
This also creates a property in the B entity called equipment that can be used to get all the A entities that reference the particular B.
Suppose you have a B entity for broken equipment in a variable broken. You can get all the broken equipment this way
broken.equipment.get()
This is also available with ndb but the details are a little different and you'll need to check the docs for that.

GAE Datastore query by an entity's parent kind

Is there a way how to query all GAE Datastore entities that have a parent of a given kind? Each entity has a key that consists of a kind and id/name and we would like to query by that kind. Is this somehow possible to use that information in a query? Or do we have to store the kind in a separate property and then use that property in the query?
That's an interesting question. If you mean, given an Entity of kind A, where A's parent can be of kind B, C, ..., find all of the A's that have a parent of kind B, then I'm pretty sure that the answer is that this isn't doable in a single query, other than iterating across all As, examining their parent's kind. (If I discover otherwise, I'll revise this answer).
Given this problem, I'd store the parent kind as a separate (string) property.

How do I model a many-to-many relationship in AppEngine's Datastore in Go?

I'm trying to wrap my head around how I can represent a many-to-many relationship inside of AppEngine's Datastore in the Go Programming Language. I'm more used to traditional relational databases.
I have two types of entities in my system. Let's call them A and B. Every A entity is related to some number of B entities. Similarly, every B entity is related to some other number of A entities. I'd like to be able to efficiently query for all B entities given an A entity, and for all A entities given a Bentity.
In the Python SDK, there seems to be a way to note fields in an entity can be ReferencePropertys which reference some other entity. However, I can't find something similar in Go's AppEngine SDK. Go seems to just use basic structs to represent entities.
What's the best practice for dealing with this?
A python ReferenceProperty essentially stores a key to another entity. It's similar to using a Key field in Go.
There's at least two ways to solve your problem. A cheap way to store a limited number of references, and an expensive way for larger data sets.
fmt.Println.MKO provided the answer for the cheap way, except the query is simpler than what he suggests, it should actually be:
SELECT * FROM B where AIds = 'A1'
This method is limited to the number of indexed entries per entity, as well as the entity size. So the list of AIds or BIds will limit the number of entities to 20000 or less.
If you have an insane amount of data, you would want a mapping entity to represent the M2M relationship between a given A & B entity. It would simply contain a key to an A and a key to a B. You would then query for map entities, and then fetch the corresponding A or B entities you need. This would be much more expensive, but breaks past the entity size limit.
based on how you which to query you could do the following:
in your struct A add a field:
BIds []int64
in your struct B add a field:
AIds []int64
now any time you add a relation between A and B you just need to add the corresponding ids to your two variables
when you need to query now for all B which are related to this A1 you do your query like this:
SELECT * FROM B where AIds = 'A1'
for all A wich are related to this B1 your do it similar:
SELECT * FROM A where BIds = 'B1'
update:
altered querys on suggestion from dragonx

Is a Django ManyToMany add() an append function?

If my models.py has a ManyToMany relationship between books and authors, and if for a particular SampleBook I execute:
Sample_book.authors.add(author1)
Sample_book.authors.add(author2)
Sample_book.authors.add(author3)
are author1, author2, and author3 stored in books.authors.all in the order in which they were added?
i.e. is the ManyToMany add() function similar to an append? If I try to extract the values in a for loop, will they be returned in the order they were initially added?
Continued:
The answer received below stated that the db/ django did not bear responsibility for the order in which the objects were stored.
The problem is that I have a nested sort problem. e.g. I send over a list of books to the template, using order_by to sort it. But the template needs to display all the books, as well as all authors for each book, with the authors sorted as well.
Since there is a ManyToMany relationship between books and authors, the authors for each book are not necessarily stored in order (hence my original question). So the template displays books in the order passed, and I used regroup as a hack to sort the authors retrieved by association from each book.
Does anybody have a more elegant solution?
The relational database makes no guarantee of ordering.
Django can't make a guarantee, either, since it depends on the underlying relational database.
If you want a specific ordering, you must implement that specific ordering by providing some kind of sequence number.
Usually order_by is simplest, Since it's part of the query set. See http://docs.djangoproject.com/en/1.2/ref/models/querysets/#order-by-fields
The fastest way is to create a list and use sorted in the view function.
object_list = sorted( some_query_set, key=lambda o: o.some_field )
Or
object_list= list( some_query_set )
object_list.sort( key=lambda o: o.some_field )
Either of these will be really fast.

Resources