DEV Community

marknosal
marknosal

Posted on

Association Creator Method

Intro

SQLAlchemy is an Object Relational Mapper (ORM) used as a link between Python objects and database records. Since Python is Object Oriented and databases are relational it allows Python code to be easily converted to SQL. This in turn makes the interaction between the two realms seamless. Since it uses a Pythonic syntax, developers can write database queries in code they are familiar with.

Association Proxy

With some experience in coding models in SQLAlchemy you'll have noticed relationships between them can become very intricate very quickly, escpecially in a many2many relationship. The association proxy is a wonderful toolthat was developed to simplify the complexity by untangling the extra steps you have to go through to create a many2many relationship. The proxy's come in handy also allows for easier management of attributes and code readability, but its helpfulness in many2many relationships is where you'll most likely see its advantage.
Here is an example of crating a many2many without utilizing the association proxy. One way requires you to create a seperate association table to link the two models:

Image description

Here is the same many2many relationship, but utilizing SQLAlchemy's association proxy instead:

Image description

As you can see the association proxy creates a more readable and concise way to link your many2many relationship. You can use in many ways such as linking a user to items through the user's cart (association proxy).

The Creator Method

Once you have gotten familiar with the association proxy you can learn about another one of its many uses, the creator method. It is a pivotal feature of the proxy that brings another dimension to your relationship mangement needs. It really shines where relationships need to dynamivally created during your application's runtime. The association proxy first takes the relationship the model is connected too, then it takes the attribute of the connected model that is connected to the other side of the many2many, the last bit is the creator argument. The creator argument takes instructions for how to create the instance of the intermediary model. Typically this is a lambda function.
Here is another example when Many book readers can own the same book and many books have many bookreaders through the book reader's bookshelves:

Image description

Here you can see when you add an instance of a model's relationship to their association proxy it will create a new instance of the connecting model (in this case a bookshelf) with the corresponding foreign keys.

Summary

We learned that SQLAlchemy plays a huge role as an Object Relational Mapper in Python application development. It helps smooth the interactions between developer's Python objects and their relational databases. Further we were shown one of its tools, the association proxy, that helps simplify complex many2many relationships in an elegant way. Lastly we explored the association proxy's creator method. Which helped in showing us a new way to create create and manage our database records and python object instances that link them.

Top comments (0)