<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bry</title>
    <description>The latest articles on DEV Community by Bry (@brythewiseguy).</description>
    <link>https://dev.to/brythewiseguy</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1082123%2F8973f546-d955-468f-89e4-924b7d8921c4.jpeg</url>
      <title>DEV Community: Bry</title>
      <link>https://dev.to/brythewiseguy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/brythewiseguy"/>
    <language>en</language>
    <item>
      <title>Using Flask-Marshmallow for easy JSON Serialization</title>
      <dc:creator>Bry</dc:creator>
      <pubDate>Mon, 09 Oct 2023 03:20:48 +0000</pubDate>
      <link>https://dev.to/brythewiseguy/flask-easy-mode-using-flask-marshmallow-for-serialization-3bbo</link>
      <guid>https://dev.to/brythewiseguy/flask-easy-mode-using-flask-marshmallow-for-serialization-3bbo</guid>
      <description>&lt;p&gt;I think it's safe to say everyone likes to make things in their lives just a little bit easier. During the past two weeks, I've been developing a full stack application that utilizes the Flask framework for Python and SQLAlchemy as it's backend. When I first started to think about my model structures, I was curious how I was going to control what these would look like when transformed into the JSON used by my frontend. I tried to use Serializer Mixin, but with so many interconnected bi-directional relationships the need for multiple serialization rules just made things really messy.&lt;/p&gt;

&lt;h2&gt;
  
  
  In Comes Flask-Marshmallow!!
&lt;/h2&gt;

&lt;p&gt;Flask-Marshmallow made things much more simple, clean, and easy to understand. It's going to be my go-to serializer for the time being, simply because of how easy it made controlling what my JSON responses included and how it was integrated into my Flask routes. Today I am here to take you through a semi-brief walkthrough on the in's and out's of creating schema's for two models and a simple join table, and how we utilize Flask-Marshmallow to shape our JSON responses to frontend requests. I will not be going over the specifics on how to set up the models or the join table - I have a separate blog post &lt;a href="https://dev.to/brythewiseguy/creating-join-tables-with-sqlalchemy-e22"&gt;HERE&lt;/a&gt; that goes through the steps to create models and a join table with SQLAlchemy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;First things first: Obviously we are working with Python and Flask, but we will need to install the necessary dependencies in order to use Flask-Marshmallow. Since I am using SQLAlchemy to define my models, I installed both Flask-Marshmallow and Marshmallow-SQLAlchemy. Using a Python Virtual Environment, you can run the following command to install both simultaneously:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pipenv install Flask-Marshmallow SQLAlchemy-Marshmallow&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Once these are installed, there is an import and variable setup we need to utilize Marshmallow in our app. Wherever you decide to define your schema's, you will need to do two things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;from flask_marshmallow import Marshmallow&lt;/code&gt; - This will import the Marshmallow class that will help Marshmallow integrate with Flask&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;ma = Marshmallow(app)&lt;/code&gt; - This sets up the Marshmallow instance for your Flask app. The &lt;code&gt;ma&lt;/code&gt; variable will be used to setup our schema's! Be sure you import your &lt;code&gt;app&lt;/code&gt; to be used here!&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faq8otkzusygx2pcoski9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faq8otkzusygx2pcoski9.png" alt="Dependency Imports"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Models
&lt;/h2&gt;

&lt;p&gt;For this example, I will be using 3 models: a &lt;code&gt;User&lt;/code&gt;, a &lt;code&gt;Game&lt;/code&gt;, and a &lt;code&gt;UserLibrary&lt;/code&gt; model. I'm not going to go into depth on these - all 3 are relatively straight forward SQLAlchemy models that are similar to the models I outline in my previous blog post:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fswksmwb4yx8ay5w1okio.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fswksmwb4yx8ay5w1okio.png" alt="User Model"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6k3z34fx9wkeb8o5o9o9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6k3z34fx9wkeb8o5o9o9.png" alt="Game Model"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6yiisju8907ryki56xyp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6yiisju8907ryki56xyp.png" alt="UserLibrary Model"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Schema's
&lt;/h2&gt;

&lt;p&gt;The coolest part about Flask-Marshmallow for me was setting up my schema's. The schema's offer a pretty intuitive way to self define what we send to our frontend through JSON serialization. This makes it really easy to manage things like sensitive user data, or data that only needs to be utilized on the backend of your application. The best part is that they are defined very similar to how a SQLAlchemy model is defined, with some obvious differences. I'm going to breakdown my &lt;code&gt;User_Schema&lt;/code&gt; as an example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcw8h2jvoz8mi7dbun9q1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcw8h2jvoz8mi7dbun9q1.png" alt="User_Schema"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In our user schema, we have defined all of the data we want to send back to the frontend side of our application about the user. We could change this if, for example, we did not want to send back the user's email to the frontend. Maybe we only want that data to be privately stored on the backend - it's as easy as not including it in your schema. &lt;/p&gt;

&lt;p&gt;We define our schema, passing in &lt;code&gt;ma.SQLAlchemySchema&lt;/code&gt; as an argument - this tells Marshmallow that it should inherit from a SQLAlchemy model. The &lt;code&gt;User_Schema&lt;/code&gt; then defines a class instance &lt;code&gt;Meta&lt;/code&gt; that is linked to our &lt;code&gt;User&lt;/code&gt; model. This is used to associate the schema with the model. We then define all of the data we want included in the JSON response. &lt;code&gt;ma.auto_field()&lt;/code&gt; is a quick and easy way to define the data types of each attribute - it allows Marshmallow to automatically infer the field type based on the data type of the attribute in the schema. You'll notice for the library attribute, we are using a different attribute definition &lt;code&gt;ma.Pluck&lt;/code&gt;. We'll get into this soon, don't worry!!&lt;/p&gt;

&lt;p&gt;Below the schema we have defined two variables, each of which has a different use case for the schema instance. The &lt;code&gt;singular_user_schema&lt;/code&gt; is used any time we want the response to be for a singular user. Alternatively, the &lt;code&gt;multiple_user_schema&lt;/code&gt; is used whenever we need to return multiple users in our response - this is accomplished by passing &lt;code&gt;many=true&lt;/code&gt; to our schema instance. My &lt;code&gt;Game_Schema&lt;/code&gt; is very similar:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2bpw44xyvmljprrnas2u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2bpw44xyvmljprrnas2u.png" alt="Game_Schema"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Association Schema
&lt;/h2&gt;

&lt;p&gt;Now to the fun part!! I know you are pretty curious about that &lt;code&gt;ma.Pluck&lt;/code&gt; attribute definition and this is where that comes into play. Below is our &lt;code&gt;User_Library_Schema&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpfwgizsx1z97sczrvv26.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpfwgizsx1z97sczrvv26.png" alt="User_Library_Schema"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This schema looks relatively simple. You'll notice one small difference though: the &lt;code&gt;game&lt;/code&gt; attribute is defined with &lt;code&gt;ma.Nested(singular_game_schema)&lt;/code&gt;. This tells Marshmallow to go find our game schema, and nest that schema into our user library schema. If you recall, the &lt;code&gt;UserLibrary&lt;/code&gt; model is a join table that specifically houses a &lt;code&gt;game&lt;/code&gt; and a &lt;code&gt;user&lt;/code&gt; - this is the schema that helps facilitate the relationship between the two. When we pull in data for each user's library, we want all of their games nested into a nicely packaged &lt;code&gt;UserLibrary&lt;/code&gt; record on our backend.&lt;/p&gt;

&lt;p&gt;Now going back to our &lt;code&gt;User&lt;/code&gt; model and our &lt;code&gt;User_Schema&lt;/code&gt;. In our &lt;code&gt;User&lt;/code&gt; model, we established a bi-directional relationship with the &lt;code&gt;UserLibrary&lt;/code&gt; model through the &lt;code&gt;library&lt;/code&gt; attribute. Each user will have multiple library entries on our backend. &lt;code&gt;library = ma.Pluck("User_Library_Schema", 'game', many=True)&lt;/code&gt; takes our library attribute, hops over to our &lt;code&gt;User_Library_schema&lt;/code&gt; and "plucks" the game objects out of it for the associated user. It then nests those games inside of the library attribute that we are sending back with our user JSON data to the frontend. Here's a visual representation of what that response might look like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fso0y454kk5xignikt20o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fso0y454kk5xignikt20o.png" alt="JSON Response"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, the serialization of the user data gets sent back a library that is an array containing all of the game objects associated with our user. Pretty dang cool if you ask me!!&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Phew! This has been a long blog post, and I hope you've made it this far! Flask-Marshmallow is a really powerful tool that gives us a lot more control over our data serialization, and makes it a lot more intuitive and easier to understand when we are in the thick of creating a complex database structure. This honestly saved my life when I was working on my most recent project. I was so lost in complex serialization rules, I almost lost track of which rule needed to be defined where. I honestly highly advise everyone to check this tool out if you haven't already - I promise you it makes creating a Flask-SQLAlchemy backend a lot easier. Out with those pesky serialization rules!! I'm going to include my full repo for the related project below - please feel free to check it out, as I have a LOT more schema's than the one's I used as examples here. Happy Coding y'all!&lt;/p&gt;

&lt;p&gt;GitHub Repo: &lt;a href="https://github.com/BryTheWiseGuy/re-flex-games-app" rel="noopener noreferrer"&gt;https://github.com/BryTheWiseGuy/re-flex-games-app&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>A Quick Guide: Creating Join Tables with SQLAlchemy</title>
      <dc:creator>Bry</dc:creator>
      <pubDate>Sun, 27 Aug 2023 01:46:17 +0000</pubDate>
      <link>https://dev.to/brythewiseguy/creating-join-tables-with-sqlalchemy-e22</link>
      <guid>https://dev.to/brythewiseguy/creating-join-tables-with-sqlalchemy-e22</guid>
      <description>&lt;p&gt;I recently created a CLI using Python and SQLAlchemy that required the use of a join table. For those of you that have experience on this topic, creating a normal model with SQLAlchemy is a pretty simple task. However, when you start to join those models in many-to-many relationships things can get complex pretty quickly. Implementing foreign keys tied back to all your original models, and then interacting with the join table in the database can quickly lead to many confusing error messages if not set up correctly. During my experience creating a CLI, this was by far one of the more frustrating and confusing tasks. I find writing Python functions to be pretty straight-forward in comparison. In this blog post, I am going to provide a quick run-down of how to create a simple join table using SQLAlchemy and explain the logistics behind it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;Before getting started with creating models, there are a few imports we will need to ensure we can format our models the way we want and define what data a table record should have.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Hbj7vUaL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ovgo6i56fqh9r312xvz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Hbj7vUaL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ovgo6i56fqh9r312xvz.png" alt="sqalchemy imports" width="637" height="115"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Your &lt;code&gt;sqlalchemy&lt;/code&gt; imports may look different depending on the needs of your model, but &lt;code&gt;relationship&lt;/code&gt; and &lt;code&gt;declarative_base&lt;/code&gt; are necessities to establish base models and table relationships. SQLAlchemy's &lt;code&gt;declarative_base&lt;/code&gt; has some very powerful features that simplify the object relational mapping necessary for any database framework. The declarative base serves as a base class for database models, and combines table definitions and class definitions. It simplifies class-table mapping, provides access to database queries, and integrates sessions to perform CRUD operations on our database. &lt;/p&gt;

&lt;h2&gt;
  
  
  Building Base Models
&lt;/h2&gt;

&lt;p&gt;Building the base models is a relatively simple task, as they look a lot like vanilla Python classes. The following are the models I created:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8uD-ndk6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/70dum5twn5gv1fczebz3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8uD-ndk6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/70dum5twn5gv1fczebz3.png" alt="base game/user models" width="738" height="716"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Table names are defined using the SQLAlchemy attribute &lt;code&gt;__tablename__&lt;/code&gt; accessible through &lt;code&gt;Base&lt;/code&gt;. We then can define all of our columns for the database table. This also serves as a model for the creation of class instances, which will inherit attributes from the Base. We lastly define a magic method &lt;code&gt;__repr__&lt;/code&gt; (representation), that formats instances of created classes into human readable strings. This is extremely helpful when debugging code with &lt;code&gt;ipdb&lt;/code&gt; or in a situation where you need to &lt;code&gt;print()&lt;/code&gt; an instance of a class to the console for any reason.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building the &lt;code&gt;user_library&lt;/code&gt; Join Table
&lt;/h2&gt;

&lt;p&gt;Building the join table is quite a bit different from creating simple models. As the join table will serve as an "in between" for our many-to-many relationships between the &lt;code&gt;users&lt;/code&gt; and &lt;code&gt;games&lt;/code&gt; table, we will need to implement the use of foreign keys. This is how I have modeled my join table:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Y0FGUV9S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/78sdxcgyqju0d5r3gd8t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Y0FGUV9S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/78sdxcgyqju0d5r3gd8t.png" alt="user_library join table" width="668" height="185"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In my join table, I have defined foreign keys as &lt;code&gt;user_id&lt;/code&gt; linking back to the &lt;code&gt;users&lt;/code&gt; table, and &lt;code&gt;game_id&lt;/code&gt; linking back to the &lt;code&gt;games&lt;/code&gt; table. We then need to establish a relationship between a &lt;code&gt;User&lt;/code&gt; class instance and a &lt;code&gt;Game&lt;/code&gt; class instance through our join table. We do this by using the &lt;code&gt;relationship()&lt;/code&gt; function built into &lt;code&gt;sqlalchemy.orm&lt;/code&gt;. We define &lt;code&gt;user&lt;/code&gt; as a relationship to a &lt;code&gt;User&lt;/code&gt; class instance that back populates to the &lt;code&gt;user_library&lt;/code&gt; table. The &lt;code&gt;back_populates&lt;/code&gt; argument is used to establish a bidirectional relationship between a &lt;code&gt;User&lt;/code&gt; and &lt;code&gt;user_library&lt;/code&gt;. We do the same exact thing for the &lt;code&gt;Game&lt;/code&gt; class instance relationship.&lt;/p&gt;

&lt;h2&gt;
  
  
  Defining Relationships to &lt;code&gt;user_library&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The last step to establishing our many-to-many relationship join table is defining a &lt;code&gt;user_library&lt;/code&gt; attribute in both our &lt;code&gt;Game&lt;/code&gt; and &lt;code&gt;User&lt;/code&gt; models. It is very important to pay attention to syntax here, as defining these relationships incorrectly can lead to unexpected bugs or interactions with your database. This is how I have updated my models:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Mmnsth9R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sx14iktchwslpf6xuswe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Mmnsth9R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sx14iktchwslpf6xuswe.png" alt="refactored game/user models" width="735" height="804"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because we are linking foreign keys for both Game and User instances, we need to define a &lt;code&gt;user_library&lt;/code&gt; connection in both models. It's important that the name definition for the class attribute matches the name of the join table. In my example I defined my class attribute as &lt;code&gt;user_library&lt;/code&gt; which is the same as the connecting join table. Looking at the &lt;code&gt;User&lt;/code&gt; model as the example, we use the &lt;code&gt;relationship&lt;/code&gt; function to establish a relationship to instances of &lt;code&gt;User_library&lt;/code&gt; and then use &lt;code&gt;back_populates&lt;/code&gt; to establish a bidirectional relationship to the &lt;code&gt;user&lt;/code&gt; attribute we defined in the &lt;code&gt;User_library&lt;/code&gt; model. I've drawn arrows in the below image to more clearly exhibit the relationships we established.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--09I8MPWN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sgtetnkv7cj6zhw3rcs0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--09I8MPWN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sgtetnkv7cj6zhw3rcs0.png" alt="relationship connections" width="800" height="1131"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;That is a basic tutorial on how to build a join table and establish table relationships. Trust me, this process can get extremely complex when working with more than 3-4 tables and/or multiple join tables. I created multiple Entity Relationship Diagrams during the process of creating my CLI that looked like jumbled up spaghetti, but it was extremely rewarding once I figured it out. Hopefully I've been able to help guide you through the basics of the process and help explain the why's behind it all. I'll include a link to my GitHub repo for the project that inspired this tutorial below. Happy coding everyone!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/BryTheWiseGuy/vapor-game-store-cli"&gt;Vapor Game Store CLI Repo&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>python</category>
      <category>sql</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>React.js State: Dynamically Controlling Components</title>
      <dc:creator>Bry</dc:creator>
      <pubDate>Sat, 01 Jul 2023 23:24:48 +0000</pubDate>
      <link>https://dev.to/brythewiseguy/reactjs-state-vs-js-context-dynamically-controlling-components-in-reactjs-4h1h</link>
      <guid>https://dev.to/brythewiseguy/reactjs-state-vs-js-context-dynamically-controlling-components-in-reactjs-4h1h</guid>
      <description>&lt;p&gt;React is a popular JavaScript library for building user interfaces. Originally created by Facebook, React is known for its declarative approach towards rendering components. The dynamic nature of applications requires a way to manage changes over time. In walks React State, one of the core concepts of React. Really understanding state and its purpose can drastically change how developers build effective, efficient, and interactive UIs. Similar to my previous experience with JavaScript context, I had a really tough time nailing &lt;code&gt;.this&lt;/code&gt; down (see what I did there?). This blog post will give a detailed breakdown of the purpose of state, why we use it, and how we use it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is React State? Why Do We &lt;code&gt;UseState()&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;"state" is an object that holds data which may change over the lifetime of a component. It allows components to create, modify, and delete properties that are internal to them, which in turn leads to dynamic changes in the rendered output. While React props allow external data passage from a parent to a child component, they don't actually change the components internal data.&lt;/p&gt;

&lt;p&gt;State represents data that can change and affect the component's behavior, therefore causing a re-render. However, unlike the DOM's state, the React state is maintained within the React ecosystem, ensuring a smoother and more efficient rendering process.&lt;/p&gt;

&lt;p&gt;Let's take an input field in a form as an example. As a user types text into an input field, we want a way to track what is being input while it is being filled out. State allows us to track that data, and even control what the user is allowed to input. Say we only want the user to be able to input 50 characters, or only be able to input a URL: State allows us to track how many characters are in the field, or require a field to include 'https://' before the form is able to be submitted. It can even be used to prompt an error message if the user attempt to submit the form outside of the guidelines we set in state.&lt;/p&gt;

&lt;p&gt;Without state, React components would be static, which isn't ideal for most applications that need to respond to user inputs. Other examples for state include a toggle button’s on/off state and data fetched from an API - both represent various states of an application.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do we &lt;code&gt;useState()&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QDWpR2IZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9bwul0unl8k1m9gjiyup.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QDWpR2IZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9bwul0unl8k1m9gjiyup.jpg" alt="react-state-example" width="475" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Above is a simple use of React State. React state is imported with the hook useState from React. Our component then declares a state by de-structuring &lt;code&gt;useState()&lt;/code&gt; into a state variable and a &lt;code&gt;setState()&lt;/code&gt; function. The &lt;code&gt;setState()&lt;/code&gt; function will allow us to set the state later on. We than set the state of the count variable with &lt;code&gt;useState()&lt;/code&gt;, setting it to '0'.&lt;/p&gt;

&lt;p&gt;Returned in our JSX, we have a button with an &lt;code&gt;onClick&lt;/code&gt; event that sets the state of count to whatever it's current state is + 1. Once the user clicks the button, the component is re-rendered and that state of the count variable is changed to 1. It's important to keep in mind that this happens asynchronously. If you were to &lt;code&gt;console.log&lt;/code&gt; count immediately after you set it's state, it's not going to reflect that state until after the component is re-rendered.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion: How Are State and Context Related?
&lt;/h2&gt;

&lt;p&gt;I like to think of state as a personal diary of a component. It's private, it changes over time, and it influences the behavior and mood of the component. Similar to how context in JavaScript sets the scope and gives us access to &lt;code&gt;.this&lt;/code&gt;, React state gives components the ability to create and manage their own context or data scope.&lt;/p&gt;

&lt;p&gt;React State is a very powerful feature that makes React a stand-out library for building user interfaces. I encourage everyone to get into a React project and play around with state hands-on to really understand its functionality. As a beginner, I was eventually able to nail this concept down in my most recent project, and you can too! Never be afraid to experiment and try news things, and as always, happy coding!&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Context in JavaScript: What is .this?</title>
      <dc:creator>Bry</dc:creator>
      <pubDate>Sun, 14 May 2023 22:38:31 +0000</pubDate>
      <link>https://dev.to/brythewiseguy/context-in-javascript-what-is-this-po3</link>
      <guid>https://dev.to/brythewiseguy/context-in-javascript-what-is-this-po3</guid>
      <description>&lt;h3&gt;
  
  
  Learning Code is Like Doing a Puzzle
&lt;/h3&gt;

&lt;p&gt;For the past 12 weeks, I've started to embark on my journey into learning how to code in JavaScript and HTML. It's been a wild journey to making my first web application, one that I can only compare to completing a jigsaw puzzle. You see, completing a jigsaw puzzle requires not only having all the pieces, but figuring out how to put them together and knowing what the big picture looks like. Sometimes it takes a lot of time and can be extremely frustrating when your trying to find that one piece, but the end result is rewarding. Coding is the same way: understanding all of the pieces, putting them all together, and realizing the big picture is all integral into developing an application. I can say without a doubt that the one puzzle piece I still struggle with as a new developer, is the concept of context. It's the one piece of the puzzle I had to ask multiple questions: What is &lt;code&gt;this&lt;/code&gt;, why is &lt;code&gt;this&lt;/code&gt;, where is &lt;code&gt;this&lt;/code&gt;, when is &lt;code&gt;this&lt;/code&gt;?&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Context in JavaScript?
&lt;/h3&gt;

&lt;p&gt;In technical terms, context refers to the object to which a function belongs or is bound. It determines how the function is executed and how it can access variables, properties, and methods within its scope. The confusing part, however, is that context changes dynamically depending on when and how it is used.&lt;/p&gt;

&lt;p&gt;There are two main types of context in JavaScript: Global and Function context. The global context is the default context in JavaScript. It represents the context of the entire program. This is definitely the most simplified state of context: When you define a function, object, or variable in the global context, it can be accessed anywhere in the program. &lt;/p&gt;

&lt;p&gt;Functional context on the other hand, refers to the context within a specific function. Every function created in a program has its own internal context, with direct access to the global context. Where this can get &lt;em&gt;&lt;strong&gt;really&lt;/strong&gt;&lt;/em&gt; confusing, is when you have functions nested inside of other functions, or used as methods on objects.&lt;/p&gt;

&lt;p&gt;In comes the concept of the keyword &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Big 4
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;this&lt;/code&gt; keyword in JavaScript provides a reference to the current context that can allow a function to access and manipulate the methods and properties of the context object. This changes dynamically: where the function is invoked matters, and where the &lt;code&gt;this&lt;/code&gt; keyword is used matters.&lt;/p&gt;

&lt;p&gt;I like to think of context like an everyday office building: &lt;code&gt;this&lt;/code&gt; person works at &lt;code&gt;this&lt;/code&gt; office building, on &lt;code&gt;this&lt;/code&gt; floor,  in &lt;code&gt;this&lt;/code&gt; suite, in &lt;code&gt;this&lt;/code&gt; cubicle. The value of &lt;code&gt;this&lt;/code&gt; depends on where you use it and what you are referring to.&lt;/p&gt;

&lt;p&gt;There are 4 main ways the value of &lt;code&gt;this&lt;/code&gt; can be determined:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Function Invocation:&lt;/strong&gt; When a function is defined as a standalone function without any specified context, &lt;code&gt;this&lt;/code&gt; refers to the global context. Here's an example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyctak99g0ypxus4vgsn4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyctak99g0ypxus4vgsn4.jpg" alt="Function Invocation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We define the name, John, as a variable in the global context. Inside the &lt;code&gt;greet()&lt;/code&gt; function, we can access that name via dot notation with the &lt;code&gt;this&lt;/code&gt; keyword. To go back to the office analogy, think of the 'greet()` function as the office building, and our name variable as a specific person in a pool of people that work there (in the global scope). What if you want to go further, and access the value within an object key?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Method Invocation:&lt;/strong&gt; When we call a function as a method on an object, the context of that function now refers to the object that owns the method. The context of &lt;code&gt;this&lt;/code&gt; is now implicitly set to the specific object:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6e24qin5yfw09e87twhy.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6e24qin5yfw09e87twhy.jpg" alt="Method Invocation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, the context of &lt;code&gt;this&lt;/code&gt; never leaves the object; &lt;code&gt;this&lt;/code&gt; will always refer to the object keys available within the object itself when called inside the &lt;code&gt;greet()&lt;/code&gt; method. If the office is the global scope (everything outside of the object) and the object is the suite, then the object key become the cubicles, and the value becomes the person in that cubicle, in this case 'Alice'. What if we want to add a new suite altogether, with new cubicles for new people?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Constructor Invocation:&lt;/strong&gt; Constructor functions are probably one of the coolest things in coding, and are one of the more digestible ways to understand the concept of context. When a function is used as a constructor with the &lt;code&gt;new&lt;/code&gt; keyword, &lt;code&gt;this&lt;/code&gt; refers to the newly created instance of the object. The context of &lt;code&gt;this&lt;/code&gt; is automatically set to the newly created object.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbxld0tjg12mgjxbgaa8n.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbxld0tjg12mgjxbgaa8n.jpg" alt="Constructor Invocation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, the &lt;code&gt;person()&lt;/code&gt; constructor function is being used to create entirely new objects, where &lt;code&gt;this&lt;/code&gt; has context within each new object created; the context never overlaps between objects and is specific to the object being created. We utilize the &lt;code&gt;new&lt;/code&gt; keyword to create a &lt;em&gt;&lt;strong&gt;new&lt;/strong&gt;&lt;/em&gt; person, wherein &lt;code&gt;this&lt;/code&gt; references the creation of the name key/value pairing. You could even create more than just a name: &lt;code&gt;this.age&lt;/code&gt; or &lt;code&gt;this.height&lt;/code&gt;. Moving away from the office analogy, think of this one in terms of the details of individual people, and how the value of name, age, and height would change depending on the context of who you are referring to.&lt;/p&gt;

&lt;p&gt;There is one last main method of invocation called explicit binding, and for the sake of the length of this blog post, I'm going to save that for another time. But you can imagine how utilizing &lt;code&gt;this&lt;/code&gt; can be a very powerful tool. Although it can be confusing, it's all about thinking about what scope you are in, and tracing &lt;code&gt;this&lt;/code&gt; back to it's reference. Sometimes working backwards can be beneficial.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion: The Power of .this
&lt;/h3&gt;

&lt;p&gt;Understanding context can be very beneficial to writing effective and DRY JavaScript code. It reduces the need for a lot of repetition in your code and, for a newbie like myself, can really help you to understand how functions interact with objects. I recommend starting small with context, and slowly building bigger with experience. While context isn't completely necessary in writing code, and can make things a &lt;em&gt;&lt;strong&gt;lot&lt;/strong&gt;&lt;/em&gt; more confusing if not fully grasped, it can give you greater control over your code's behavior and provide some flexibility. Remember that context changes dynamically, and it is essential to consider the context in which a function operates when attempting to work &lt;code&gt;this&lt;/code&gt; into your code. &lt;/p&gt;

&lt;p&gt;Now get out there and try something new, and remember to never give up! Practice makes perfect, and that could never be more true than in developing skills in coding. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>frontend</category>
      <category>coding</category>
    </item>
  </channel>
</rss>
