How do ‘primaryjoin’ and ‘secondaryjoin’ work for many-to-many relationship in SQLAlchemy?

In a many to many relationship, the primaryjoin expression describes the join between the left table and the junction table, and the secondaryjoin describes the join between the junction table and the right table. In other words, the primaryjoin expression is saying, “find all rows in the followers table where follower_id is X”, the secondaryjoin … Read more

MySQL – How to insert into table that has many-to-many relationship

Here is what i ended up doing. I hope it helps someone. INSERT INTO persons (firstname,lastname) VALUES (‘John’,’Doe’); SET @person_id = LAST_INSERT_ID(); INSERT IGNORE INTO properties (property) VALUES (‘property_A’); SET @property_id = LAST_INSERT_ID(); INSERT INTO has_property (person_id,property_id) VALUES(@person_id, @property_id); INSERT IGNORE INTO properties (property) VALUES (‘property_B’); SET @property_id = LAST_INSERT_ID(); INSERT INTO has_property (person_id,property_id) VALUES(@person_id, … Read more

Entity Framework 4.1+ many-to-many relationships change tracking

Here is how to find all the changed many-to-many relationships. I’ve implemented the code as extension methods: public static class IaExtensions { public static IEnumerable<Tuple<object, object>> GetAddedRelationships( this DbContext context) { return GetRelationships(context, EntityState.Added, (e, i) => e.CurrentValues[i]); } public static IEnumerable<Tuple<object, object>> GetDeletedRelationships( this DbContext context) { return GetRelationships(context, EntityState.Deleted, (e, i) => e.OriginalValues[i]); … Read more

Removing many to many entity Framework

Standard way is to load the artist including the current related types from the database and then remove the types with the selected Ids from the loaded types collection. Change tracking will recognize which types have been removed and write the correct DELETE statements to the join table: var artist = this._db.Artists.Include(a => a.ArtistTypes) .SingleOrDefault(a … Read more

django 1.4 Many-to-Many bulk add

If you want to add queryset to bulk add or remove method of many to many relation models : qs = Article.objects.all() publications = Publications.objects.get(id=1) publications.article_set.add(*qs) publications.save() publications.article_set.remove(*qs) publications.save()

SqlAlchemy and Flask, how to query many-to-many relationship

The semantic of the relationship doesn’t look right. I think it should be something like: class Restaurant(db.Model): … dishes = db.relationship(‘Dish’, secondary=restaurant_dish, backref=db.backref(‘restaurants’)) Then, to retrieve all the dishes for a restaurant, you can do: x = Dish.query.filter(Dish.restaurants.any(name=name)).all() This should generate a query like: SELECT dish.* FROM dish WHERE EXISTS ( SELECT 1 FROM restaurant_dish … Read more

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