Many-to-many relationships examples

Example scenario: students and courses at a university. A given student might be on several courses, and naturally a course will usually have many students. Example tables, simple design: CREATE TABLE `Student` ( `StudentID` INT UNSIGNED NOT NULL AUTO_INCREMENT, `FirstName` VARCHAR(25), `LastName` VARCHAR(25) NOT NULL, PRIMARY KEY (`StudentID`) ) ENGINE=INNODB CHARACTER SET utf8 COLLATE utf8_general_ci … Read more

How to build many-to-many relations using SQLAlchemy: a good example

From the comments I see you’ve found the answer. But the SQLAlchemy documentation is quite overwhelming for a ‘new user’ and I was struggling with the same question. So for future reference: ItemDetail = Table(‘ItemDetail’, Column(‘id’, Integer, primary_key=True), Column(‘itemId’, Integer, ForeignKey(‘Item.id’)), Column(‘detailId’, Integer, ForeignKey(‘Detail.id’)), Column(‘endDate’, Date)) class Item(Base): __tablename__ = ‘Item’ id = Column(Integer, primary_key=True) … Read more

how to verify if object exist in manytomany

I advise you to use: if beer.salas_set.filter(pk=sala.pk).exists(): # do stuff If you use sala in beer.salas_set.all() instead, it selects all records from the relation table and loops over them to find, whether the given object is there or not. However, beer.salas_set.filter(pk=sala.pk).exists() only selects zero or one row from the database and immediately gives the result … Read more

Java method naming conventions: Too many getters

I personally don’t use getters and setters whenever it’s possible (meaning : I don’t use any framework who needs it, like Struts for instance). I prefer writing immutable objects (public final fields) when possible, otherwise I just use public fields : less boiler plate code, more productivity, less side effects. The original justification for get/set … Read more

MS SQL creating many-to-many relation with a junction table

I would use the second junction table: MOVIE_CATEGORY_JUNCTION Movie_ID Category_ID The primary key would be the combination of both columns. You would also have a foreign key from each column to the Movie and Category table. The junction table would look similar to this: create table movie_category_junction ( movie_id int, category_id int, CONSTRAINT movie_cat_pk PRIMARY … Read more