How can I achieve a self-referencing many-to-many relationship on the SQLAlchemy ORM back referencing to the same attribute?

Here’s the UNION approach I hinted at on the mailing list earlier today. from sqlalchemy import Integer, Table, Column, ForeignKey, \ create_engine, String, select from sqlalchemy.orm import Session, relationship from sqlalchemy.ext.declarative import declarative_base Base= declarative_base() friendship = Table( ‘friendships’, Base.metadata, Column(‘friend_a_id’, Integer, ForeignKey(‘users.id’), primary_key=True), Column(‘friend_b_id’, Integer, ForeignKey(‘users.id’), primary_key=True) ) class User(Base): __tablename__ = ‘users’ id … Read more

Entity Framework Code First Many to Many Setup For Existing Tables

Remove your Essence2EssenceSet model class. If junction table contains only keys of related entities participating in many-to-many relations it is not needed to map it as entity. Also make sure that your fluent mapping of many-to-many relations specifies schema for table: mb.Entity<Essence>() .HasMany(e => e.EssenceSets) .WithMany(set => set.Essences) .Map(mc => { mc.ToTable(“Essence2EssenceSet”, “Com”); mc.MapLeftKey(“EssenceID”); mc.MapRightKey(“EssenceSetID”); … Read more

Query on a many-to-many relationship using Doctrine with Symfony2

You can write a join DQL query as below $em = $this->getContainer()->get(‘doctrine’)->getManager(); $repository = $em->getRepository(‘YourNamespaceYourBundle:User’); $query = $repository->createQueryBuilder(‘u’) ->innerJoin(‘u.groups’, ‘g’) ->where(‘g.id = :group_id’) ->setParameter(‘group_id’, 5) ->getQuery()->getResult(); Your mapping for groups property in User entity will handle join part itself you don’t have to mention the junction table in your DQL query

How to define Many-to-Many relationship through Fluent API Entity Framework?

The terms Left and Right in MapLeftKey and MapRightKey in the many-to-many mapping with Fluent API can be misunderstood and I guess your problem is caused by this misunderstanding. One might think that it means they describe the columns that are “left” and “right” in the many-to-many join table. That’s actually the case if you … Read more

Generic many-to-many relationships

You can implement this using generic relationships by manually creating the junction table between message and recipient: from django.db import models from django.contrib.contenttypes import generic from django.contrib.contenttypes.models import ContentType class Client(models.Model): city = models.CharField(max_length=16) # These aren’t required, but they’ll allow you do cool stuff # like “person.sent_messages.all()” to get all messages sent # by … Read more

How to do many-to-many Django query to find book with 2 given authors?

Not intuitive at first but the answer is right in front of us. Book.objects.filter(author__id=1).filter(author__id=2) If you want an exact match, you could potentially further filter this result by those items that only have exactly 2 authors. Book.objects.annotate(count=Count(‘author’)).filter(author__id=1)\ .filter(author__id=13).filter(count=2) If you want exact matches dynamically, how about something like this?: def get_exact_match(model_class, m2m_field, ids): query = … Read more