<?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: Jim Grimes</title>
    <description>The latest articles on DEV Community by Jim Grimes (@jimgrimes86).</description>
    <link>https://dev.to/jimgrimes86</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%2F1174541%2F1941a978-97cd-4466-bcae-1c7a1972cecf.png</url>
      <title>DEV Community: Jim Grimes</title>
      <link>https://dev.to/jimgrimes86</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jimgrimes86"/>
    <language>en</language>
    <item>
      <title>Flask-SQLAlchemy Many-to-Many Relationships: Association Tables and Association Objects</title>
      <dc:creator>Jim Grimes</dc:creator>
      <pubDate>Tue, 21 Nov 2023 21:59:01 +0000</pubDate>
      <link>https://dev.to/jimgrimes86/flask-sqlalchemy-many-to-many-relationships-association-tables-and-association-objects-3aej</link>
      <guid>https://dev.to/jimgrimes86/flask-sqlalchemy-many-to-many-relationships-association-tables-and-association-objects-3aej</guid>
      <description>&lt;p&gt;SQLAlchemy provides two tools for managing many-to-many relationships:  Association Tables and Association Objects.  This blog will review how to use association tables and association objects in a Flask application using the Flask-SQLAlchemy extension.&lt;/p&gt;

&lt;h2&gt;
  
  
  Association Tables
&lt;/h2&gt;

&lt;p&gt;An association table, like a basic join table in SQLlite, is a database table that is used to keep track of the relationships between two other tables.  An association tables does not contain any information besides what is necessary to manage those relationships.&lt;/p&gt;

&lt;p&gt;Say we have two models, &lt;code&gt;Teacher&lt;/code&gt; and &lt;code&gt;Student&lt;/code&gt;, that have a many-to-many relationship.  Each teacher has many students, and each student has many teachers.  Their relationship can be managed with an association table, &lt;code&gt;teacher_students&lt;/code&gt;.  The only columns that &lt;code&gt;teacher_students&lt;/code&gt; contains are the columns that refer to the tables for &lt;code&gt;Teacher&lt;/code&gt; and Student.  &lt;code&gt;teacher_students&lt;/code&gt; cannot be used to manage any additional information.&lt;/p&gt;

&lt;p&gt;The relationship between &lt;code&gt;Teacher&lt;/code&gt; and &lt;code&gt;Student&lt;/code&gt; is created by making columns in &lt;code&gt;teacher_students&lt;/code&gt; that contain foreign keys referring to the id columns of the &lt;code&gt;Teacher&lt;/code&gt; and &lt;code&gt;Student&lt;/code&gt; tables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import MetaData
from sqlalchemy.ext.associationproxy import association_proxy

metadata = MetaData(naming_convention={
    "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
})

db = SQLAlchemy(metadata=metadata)

teacher_students = db.Table(
    'teacher_students',
    metadata,
    db.Column('teacher_id', db.ForeignKey('teachers.id'), 
        primary_key=True),
    db.Column('student_id', db.ForeignKey('students.id'), 
        primary_key=True)
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It isn't necessary to specify the data type of the columns in &lt;code&gt;teacher_students&lt;/code&gt; because the type of data in each column is inferred from the type of data in the columns of the &lt;code&gt;Teacher&lt;/code&gt; table and &lt;code&gt;Student&lt;/code&gt; table.&lt;/p&gt;

&lt;p&gt;Regarding the model columns that should be used in association table:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It is also recommended, though not in any way required by SQLAlchemy, that the columns which refer to the two entity tables are established within either a unique constraint or more commonly as the primary key constraint; this ensures that duplicate rows won’t be persisted within the table regardless of issues on the application side.&lt;br&gt;
&lt;a href="https://docs.sqlalchemy.org/en/20/orm/basic_relationships.html#many-to-many"&gt;https://docs.sqlalchemy.org/en/20/orm/basic_relationships.html#many-to-many&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To create the many-to-many relationship between &lt;code&gt;Teacher&lt;/code&gt; and &lt;code&gt;Student&lt;/code&gt;, each model is given a relationship() attribute that links them to each other.  &lt;code&gt;Teacher&lt;/code&gt; is given a &lt;code&gt;students&lt;/code&gt; attribute and &lt;code&gt;Student&lt;/code&gt; is given a &lt;code&gt;teachers&lt;/code&gt; attribute.  The connection between the models and &lt;code&gt;teacher_students&lt;/code&gt; is provided by the &lt;code&gt;secondary&lt;/code&gt; parameter in each model's relationship attribute, which points to the association table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Teacher(db.Model):
    __tablename__ = 'teachers'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)

    students = db.relationship('Student', secondary=teacher_students,
        back_populates='teachers')

    def __repr__(self):
        return f'&amp;lt;Teacher {self.id}&amp;gt;'

class Student(db.Model):
    __tablename__ = 'students'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)

    teachers = db.relationship('Teacher', secondary=teacher_students,     
        back_populates='students')

    def __repr__(self):
        return f'&amp;lt;Student {self.id}&amp;gt;'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Additional details and information about the &lt;code&gt;secondary&lt;/code&gt; parameter can be found here:&lt;br&gt;
&lt;a href="https://docs.sqlalchemy.org/en/20/orm/relationship_api.html#sqlalchemy.orm.relationship.params.secondary"&gt;https://docs.sqlalchemy.org/en/20/orm/relationship_api.html#sqlalchemy.orm.relationship.params.secondary&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To see the students that a teacher has or the teachers that a student has, the &lt;code&gt;students&lt;/code&gt; and &lt;code&gt;teachers&lt;/code&gt; attributes can be called on instances of the &lt;code&gt;Teacher&lt;/code&gt; and &lt;code&gt;Student&lt;/code&gt; models:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; t1 = Teacher.query.first()
&amp;gt;&amp;gt;&amp;gt; t1
&amp;lt;Teacher 1&amp;gt;
&amp;gt;&amp;gt;&amp;gt; t1.students
[&amp;lt;Student 1&amp;gt;, &amp;lt;Student 3&amp;gt;, &amp;lt;Student 6&amp;gt;]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; s1 = Student.query.first()
&amp;gt;&amp;gt;&amp;gt; s1
&amp;lt;Student 1&amp;gt;
&amp;gt;&amp;gt;&amp;gt; s1.teachers
[&amp;lt;Teacher 1&amp;gt;, &amp;lt;Teacher 2&amp;gt;]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Association Objects
&lt;/h2&gt;

&lt;p&gt;What if, instead of only keeping track of the relationships between &lt;code&gt;Teacher&lt;/code&gt; and &lt;code&gt;Student&lt;/code&gt; instances, we wanted the join table to also keep track of the exams that each teacher administers and each student takes.  In that case, an association object would be used instead of an association table because it is necessary for the join table to hold extra information.  In other words, when the join table needs to contain more columns than just the foreign keys from other tables, an association object should be used.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The association object pattern is a variant on many-to-many: it’s used when an association table contains additional columns beyond those which are foreign keys to the parent and child (or left and right) tables, columns which are most ideally mapped to their own ORM mapped class. &lt;br&gt;
&lt;a href="https://docs.sqlalchemy.org/en/20/orm/basic_relationships.html#association-object"&gt;https://docs.sqlalchemy.org/en/20/orm/basic_relationships.html#association-object&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the example above, there are models for &lt;code&gt;Teacher&lt;/code&gt; and &lt;code&gt;Student&lt;/code&gt;.  To create an association object that keeps track of exams, along with the relationships between &lt;code&gt;Teacher&lt;/code&gt; and &lt;code&gt;Student&lt;/code&gt; instances, there needs to be a third model, &lt;code&gt;Exam&lt;/code&gt;, which acts as the association object.  Like the association table used above, the &lt;code&gt;Exam&lt;/code&gt; model's table is still given columns to keep track of the foreign keys of the many-to-many relationship models, but it can also contain columns for whatever other information is needed to be stored in the table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Exam(db.Model):
    __tablename__ = 'exams_table'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)

    teacher_id = db.Column(db.Integer, db.ForeignKey('teachers.id'))
    student_id = db.Column(db.Integer, db.ForeignKey('students.id'))

    teacher = db.relationship('Teacher', back_populates='exams')
    student = db.relationship('Student', back_populates='exams')

    def __repr__(self):
        return f'&amp;lt;Exam {self.id}&amp;gt;'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of the &lt;code&gt;Teacher&lt;/code&gt; and &lt;code&gt;Student&lt;/code&gt; models having a direct, many-to-many relationship, they will now have one-to-many relationships with the &lt;code&gt;Exam&lt;/code&gt; association object.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In the association object pattern, the &lt;code&gt;relationship.secondary&lt;/code&gt; parameter is not used; instead, a class is mapped directly to the association table. Two individual &lt;code&gt;relationship()&lt;/code&gt; constructs then link first the parent side to the mapped association class via one to many, and then the mapped association class to the child side via many-to-one, to form a uni-directional association object relationship from parent, to association, to child. For a bi-directional relationship, four &lt;code&gt;relationship()&lt;/code&gt; constructs are used to link the mapped association class to both parent and child in both directions.&lt;br&gt;
&lt;a href="https://docs.sqlalchemy.org/en/20/orm/basic_relationships.html#association-object"&gt;https://docs.sqlalchemy.org/en/20/orm/basic_relationships.html#association-object&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Instead of &lt;code&gt;Teacher&lt;/code&gt; having a &lt;code&gt;students&lt;/code&gt; attribute, it has an &lt;code&gt;exams&lt;/code&gt; attribute, while the &lt;code&gt;Student&lt;/code&gt; model has its own &lt;code&gt;exams&lt;/code&gt; attribute.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Teacher(db.Model):
    __tablename__ = 'teachers'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)

    exams = db.relationship('Exam', back_populates='teacher')

    students = association_proxy('exams', 'student')

    def __repr__(self):
        return f'&amp;lt;Teacher {self.id}&amp;gt;'

class Student(db.Model):
    __tablename__ = 'students'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)

    exams = db.relationship('Exam', back_populates='student')

    teachers = association_proxy('exams', 'teacher')

    def __repr__(self):
        return f'&amp;lt;Student {self.id}&amp;gt;'

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means that, unlike with an association table, the relationship between &lt;code&gt;Teacher&lt;/code&gt; and &lt;code&gt;Student&lt;/code&gt; cannot be accessed directly from a relationship object in either model.  To make it easier to view the relationship between &lt;code&gt;Teacher&lt;/code&gt; and &lt;code&gt;Student&lt;/code&gt; now that they are linked by an association object, SQLAlchemy provides the Association Proxy tool.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;To enhance the association object pattern such that direct access to the Association object is optional, SQLAlchemy provides the Association Proxy extension. This extension allows the configuration of attributes which will access two “hops” with a single access, one “hop” to the associated object, and a second to a target attribute.&lt;br&gt;
&lt;a href="https://docs.sqlalchemy.org/en/20/orm/basic_relationships.html#association-object"&gt;https://docs.sqlalchemy.org/en/20/orm/basic_relationships.html#association-object&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The association proxies in the above example (&lt;code&gt;students&lt;/code&gt; in the &lt;code&gt;Teacher&lt;/code&gt; model and &lt;code&gt;teachers&lt;/code&gt; in the &lt;code&gt;Student&lt;/code&gt; model) are provided (1) the name of the immediate target that is being used to manage the many-to-many relationship and (2) an attribute that is available in an instance of that target.  In this example the immediate target is the relationship attribute &lt;code&gt;exams&lt;/code&gt;, which is the variable used to establish the one-to-many relationships between the &lt;code&gt;Teacher&lt;/code&gt; and &lt;code&gt;Student&lt;/code&gt; models and the &lt;code&gt;Exam&lt;/code&gt; association object.  The attribute given to &lt;code&gt;association_proxy()&lt;/code&gt; is the corresponding relationship variable in the &lt;code&gt;Exam&lt;/code&gt; association object.&lt;/p&gt;

&lt;p&gt;Now, the &lt;code&gt;Teacher&lt;/code&gt; and &lt;code&gt;Student&lt;/code&gt; models each have way to view the one-to-many relationship with the &lt;code&gt;Exam&lt;/code&gt; association object, as well as a way to directly view the many-to-many relationships through the association proxies.  &lt;code&gt;Teacher&lt;/code&gt;'s association proxy provides the value of the &lt;code&gt;students&lt;/code&gt; associated with it while concealing, or skipping over, the association object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; t1 = Teacher.query.first()
&amp;gt;&amp;gt;&amp;gt; t1
&amp;lt;Teacher 1&amp;gt;
&amp;gt;&amp;gt;&amp;gt; t1.exams
[&amp;lt;Exam 1&amp;gt;, &amp;lt;Exam 2&amp;gt;, &amp;lt;Exam 3&amp;gt;]
&amp;gt;&amp;gt;&amp;gt; t1.students
[&amp;lt;Student 1&amp;gt;, &amp;lt;Student 3&amp;gt;, &amp;lt;Student 6&amp;gt;]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default, calling a model's association proxy attribute will return instances of the target object.  So, calling &lt;code&gt;Teacher.students&lt;/code&gt; returns a list of all the instances of &lt;code&gt;Student&lt;/code&gt; that have a relationship with a particular instance of &lt;code&gt;Teacher&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Additional information about using association proxies can be found here:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://docs.sqlalchemy.org/en/20/orm/extensions/associationproxy.html"&gt;https://docs.sqlalchemy.org/en/20/orm/extensions/associationproxy.html&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>flask</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Using SQLite to Examine Data Relationships and Compare Tables</title>
      <dc:creator>Jim Grimes</dc:creator>
      <pubDate>Mon, 13 Nov 2023 21:26:00 +0000</pubDate>
      <link>https://dev.to/jimgrimes86/using-sqlite-to-examine-data-relationships-and-compare-tables-5fee</link>
      <guid>https://dev.to/jimgrimes86/using-sqlite-to-examine-data-relationships-and-compare-tables-5fee</guid>
      <description>&lt;p&gt;SQLite provides numerous ways to view and compare data across multiple tables.  This blog will discuss SQLite queries that can be used to view data relationships:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;INNER JOIN&lt;/li&gt;
&lt;li&gt;LEFT JOIN&lt;/li&gt;
&lt;li&gt;Using Multiple Join Operators&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And SQLite queries that can be used to compare tables:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;INTERSECT&lt;/li&gt;
&lt;li&gt;UNION&lt;/li&gt;
&lt;li&gt;UNION ALL&lt;/li&gt;
&lt;li&gt;EXCEPT&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Simple Select Queries
&lt;/h2&gt;

&lt;p&gt;In order to illustrate how to use SELECT statements, I have created a database to keep track of garden plants and garden beds, which I refer to in the examples I use.  The database has a plants table that contains information on the different garden plants, including the plant name, whether the plant requires full sun, and the time of year that the plant begins to bloom.  The data in the plants table can be retrieved with a SELECT query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM plants;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;id  name              full_sun  bloom      
--  ----------------  --------  -----------
1   Bee Balm          0         late spring
2   Petunia           1         summer     
3   Coneflower        0         summer     
4   Zinnia            1         late spring
5   Black-Eyed Susan  0         summer    
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the full_sun column, the integers 1 and 0 are used to represent the Boolean values of 'true' and 'false', respectively.&lt;/p&gt;

&lt;p&gt;To limit the result's columns to only include the plant's id number and name, the columns can be specified in the SELECT statement instead of using &lt;code&gt;*&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT id, name FROM plants;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;id  name            
--  ----------------
1   Bee Balm        
2   Petunia         
3   Coneflower      
4   Zinnia          
5   Black-Eyed Susan
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To limit the query result to only those rows that match a certain condition, a WHERE clause can be added to the query.  For example, the following query would only select plants that do not require full sunlight:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM plants WHERE full_sun IS 0;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;id  name              full_sun  bloom      
--  ----------------  --------  -----------
1   Bee Balm          0         late spring
3   Coneflower        0         summer     
5   Black-Eyed Susan  0         summer    
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Select Queries With Join Operators
&lt;/h2&gt;

&lt;p&gt;Retrieving data from a single table has its uses, but it does not allow me to easily see the relationships between data on multiple tables.  You can use join statements to view data across multiple tables at the same time.&lt;/p&gt;

&lt;p&gt;Our database also contains a table with information on garden beds:&lt;/p&gt;

&lt;h4&gt;
  
  
  beds
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;id  light        
--  -------------
1   full sun     
2   partial shade
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A type of plant can be planted in multiple garden beds, and a garden bed can have multiple kinds of plants.  To keep track of the relationships between plants and garden beds, there is a join table, called plant_beds:&lt;/p&gt;

&lt;h4&gt;
  
  
  plant_beds
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;id  plant_id  bed_id
--  --------  ------
1   1         2     
2   4         1     
3   3         2     
4   5         1     
5   1         1 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The relationship between the plants, beds, and plant_beds tables can be visualized like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_jQLz8Tx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oag8btxt8vsef3zulf76.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_jQLz8Tx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oag8btxt8vsef3zulf76.png" alt="Representation of table relationships" width="618" height="247"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  INNER JOIN
&lt;/h3&gt;

&lt;p&gt;The INNER JOIN can be used to view the plants that correspond to each row in the plant_bed join table.  SQLite treats the operators "INNER JOIN", "JOIN" and "," exactly the same, so they can be used interchangably.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT plant_beds.id, plant_beds.plant_id, plants.name, plant_beds.bed_id
FROM plant_beds
INNER JOIN plants
ON plant_beds.plant_id = plants.id
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;id  plant_id  name              bed_id
--  --------  ----------------  ------
1   1         Bee Balm          2     
2   4         Zinnia            1     
3   3         Coneflower        2     
4   5         Black-Eyed Susan  1     
5   1         Bee Balm          1    
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't forget to include the ON statement in the query.  The ON operator tells SQLite how the tables relate to each other.  Without that instruction, SQLite will return every row from the plant_beds table matched with every row from the plants database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT plant_beds.id, plant_beds.plant_id, plants.name, plant_beds.bed_id
FROM plant_beds
INNER JOIN plants
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;id  plant_id  name              bed_id
--  --------  ----------------  ------
1   1         Bee Balm          2     
1   1         Petunia           2     
1   1         Coneflower        2     
1   1         Zinnia            2     
1   1         Black-Eyed Susan  2     
2   4         Bee Balm          1     
2   4         Petunia           1     
2   4         Coneflower        1     
2   4         Zinnia            1     
2   4         Black-Eyed Susan  1     
3   3         Bee Balm          2     
3   3         Petunia           2     
3   3         Coneflower        2     
3   3         Zinnia            2     
3   3         Black-Eyed Susan  2     
4   5         Bee Balm          1     
4   5         Petunia           1     
4   5         Coneflower        1     
4   5         Zinnia            1     
4   5         Black-Eyed Susan  1     
5   1         Bee Balm          1     
5   1         Petunia           1     
5   1         Coneflower        1     
5   1         Zinnia            1     
5   1         Black-Eyed Susan  1    
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This result includes Petunias in the response, even though they aren't currently associated with any beds in our plant_beds join table.  So even though SQLite has returned information from both the plants table and the plant_beds table, the response does not represent the relationship between the two tables.&lt;/p&gt;

&lt;h3&gt;
  
  
  LEFT JOIN
&lt;/h3&gt;

&lt;p&gt;The reponse to an INNER JOIN query only includes the rows from each table that have a match in the ON statement.  SO, in the example above, Petunia is not include din the response because it is not included in the plant_beds table.  In order to see all the rows from the left table or the right table, LEFT JOIN or LEFT OUTER JOIN should be used instead of INNER JOIN.  The response from a LEFT JOIN query will include the same rows as an INNER JOIN query, as well as an extra row from each row in the left-hand table (or first table listed in the query) that does not have a mathing row in the right-hand table (the second table listed in the query).  SQLite uses NULL as a default value for any column that does not have a value in the right-hand table&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT plant_beds.id, plant_beds.plant_id, plants.name, plant_beds.bed_id
FROM plant_beds
LEFT JOIN plants
ON plant_beds.plant_id = plants.id
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;id  plant_id  name              bed_id
--  --------  ----------------  ------
5   1         Bee Balm          1     
1   1         Bee Balm          2     
              Petunia                 
3   3         Coneflower        2     
2   4         Zinnia            1     
4   5         Black-Eyed Susan  1 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As can be seen in this example, the LEFT JOIN query result includes the Petunia row from the plants table and uses NULL values, which appear as blank spaces, for the columns where there is no match for Petunia in the plant_beds table.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using a JOIN Statement With More Than Two Tables
&lt;/h3&gt;

&lt;p&gt;INNER JOIN and LEFT JOIN statements can be repeated in a query to produce a result that includes information from more than one table.  For example, to see information from the plant_beds table as well as details from both the plants and beds tables, two INNER JOIN statements can be used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT plant_beds.id, plant_beds.plant_id, plants.name, plant_beds.bed_id, beds.light
FROM plant_beds
INNER JOIN plants
ON plant_beds.plant_id = plants.id
INNER JOIN beds
ON plant_beds.bed_id = beds.id
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;id  plant_id  name              bed_id  light        
--  --------  ----------------  ------  -------------
1   1         Bee Balm          2       partial shade
2   4         Zinnia            1       full sun     
3   3         Coneflower        2       partial shade
4   5         Black-Eyed Susan  1       full sun     
5   1         Bee Balm          1       full sun 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Compound Select Statements
&lt;/h2&gt;

&lt;p&gt;What if, instead of listing all plants in one table, the plants were sorted into multiple tables based on characteristics of each plant.  I have made three more database tables to keep track of native plants, plants that are particularly attractive to bees, and plants that are likely to attract hummingbirds.  Of course, there are native plants that are known as good choices for bees and hummingbirds, so the three tables will have some plants in common.&lt;/p&gt;

&lt;h4&gt;
  
  
  native_plants
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;id  name                full_sun  bloom      
--  ------------------  --------  -----------
1   Arrowwood Viburnum  0         late spring
2   Bee Balm            0         late spring
3   Black-Eyed Susan    0         summer     
4   Coneflower          0         summer     
5   Goldenrod           0         late summer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  bee_plants
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;id  name        full_sun  bloom      
--  ----------  --------  -----------
1   Bee Balm    0         late spring
2   Lavender    1         summer     
3   Coneflower  0         summer     
4   Zinnia      1         late spring
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  hummingbird_plants
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;id  name             full_sun  bloom      
--  ---------------  --------  -----------
1   Petunia          1         summer     
2   Bee Balm         0         late spring
3   Cardinal Flower  1         mid summer 
4   Garden Phlox     0         summer 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These tables can also be visualized like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--M2T8AYNm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lukbp50ico7sordnauo4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--M2T8AYNm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lukbp50ico7sordnauo4.png" alt="Representation of multiple plant tables" width="630" height="179"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that the plants are organized into multiple tables, it would be nice to be able to compare the tables to each other.  This can be done with compound SELECT statements.  Specifically, with the INTERSECT, UNION, UNION ALL, and EXCEPT operators.&lt;/p&gt;

&lt;h3&gt;
  
  
  INTERSECT
&lt;/h3&gt;

&lt;p&gt;INTERSECT is used to obtain the columns and rows that tables have in common.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT name, bloom FROM native_plants
INTERSECT
SELECT name, bloom FROM bee_plants
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name        bloom      
----------  -----------
Bee Balm    late spring
Coneflower  summer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order for the INTERSECT query to work, the columns in each SELECT column must match.  The SELECT statements must have the same number of columns, and the columns must return the same type of information so that SQLite can compare each column value.&lt;/p&gt;

&lt;p&gt;Note that in this example, the SELECT statement is not using &lt;code&gt;id&lt;/code&gt; or &lt;code&gt;*&lt;/code&gt; to retrieve the id column from the tables.  While the native_plants, bee_plants, and hummingbird_plants tables have some plants in common, those plants do not have same id numbers in each table.  In bee_plants, Bee Balm has an id of 1 and Coneflower has an id of 3, whereas in native_plants, Bee Balm has an id 2 and Coneflower has an id of 4.  So if the query included the id column, the Bee Balm and Coneflower rows from each table would not match, and the INTERSECT operator would return no results.&lt;/p&gt;

&lt;h3&gt;
  
  
  UNION and UNION ALL
&lt;/h3&gt;

&lt;p&gt;To get the information from multiple tables, regardless of whether the tables rows are the same in each table, use the UNION operator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT name, bloom FROM bee_plants
UNION
SELECT name, bloom FROM hummingbird_plants
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name             bloom      
---------------  -----------
Bee Balm         late spring
Cardinal Flower  mid summer 
Coneflower       summer     
Garden Phlox     summer     
Lavender         summer     
Petunia          summer     
Zinnia           late spring
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The UNION operator returned the rows from the bee_plants table and the rows from the hummingbird_plants table, and it automatically removed duplicate rows.  In order to return every row from both tables, even if a row appears in each table, the UNION ALL operator can be used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT name, bloom FROM bee_plants
UNION ALL
SELECT name, bloom FROM hummingbird_plants
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name             bloom      
---------------  -----------
Bee Balm         late spring
Lavender         summer     
Coneflower       summer     
Zinnia           late spring
Petunia          summer     
Bee Balm         late spring
Cardinal Flower  mid summer 
Garden Phlox     summer 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Bee Balm appears twice in the result from the UNION ALL query because it exists in both the hummingbird_plants table and the bee_plants table.&lt;/p&gt;

&lt;h3&gt;
  
  
  EXCEPT
&lt;/h3&gt;

&lt;p&gt;The EXCEPT operator can be used to retrieve rows from one table that do not appear in another table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT name, bloom FROM native_plants
EXCEPT
SELECT name, bloom FROM bee_plants
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name                bloom      
------------------  -----------
Arrowwood Viburnum  late spring
Black-Eyed Susan    summer     
Goldenrod           late summer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Detailed documentation of SQLite's SELECT statements, including the operators discussed above and many other options for constructing SELECT statement, can be found on SQLite's website :  &lt;a href="https://sqlite.org/lang_select.html"&gt;https://sqlite.org/lang_select.html&lt;/a&gt;&lt;/p&gt;

</description>
      <category>sql</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Validating Inputs with Python Class Properties</title>
      <dc:creator>Jim Grimes</dc:creator>
      <pubDate>Sun, 05 Nov 2023 22:04:16 +0000</pubDate>
      <link>https://dev.to/jimgrimes86/validating-inputs-with-python-class-properties-gh</link>
      <guid>https://dev.to/jimgrimes86/validating-inputs-with-python-class-properties-gh</guid>
      <description>&lt;h2&gt;
  
  
  Using Properties to Validate an Input at the Time a Class Instance is Created
&lt;/h2&gt;

&lt;p&gt;The purpose of this blog post is to discuss some fundamental guidelines for creating class properties, namely: (1) the need to use getter and setter functions to validate inputs, and (2) the use of different variables for class properties and attributes.  I did not have a clear understanding of those two things when I was first learning how to use class properties in python, so my attempts to use class properties were more frustrating than they needed to be.  In my discussion of these points, I will refer to the following example: &lt;/p&gt;

&lt;h4&gt;
  
  
  Example 1:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Person:

    def __init__(self, name):
        self.name = name

    def get_name(self):
        return self._name

    def set_name(self, new_name):
        if type(new_name) == str and len(new_name) &amp;gt; 0:
            self._name = new_name

    name = property(get_name, set_name)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example 1 establishes a Person class which is meant to satisfy these conditions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An instance of the Person class must be given a name at the time of creation.&lt;/li&gt;
&lt;li&gt;The property functions are used to ensure that a person's name is a string containing at least one character.&lt;/li&gt;
&lt;li&gt;Calling the person's name should return the person's name.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If an instance of the Person class is created with a valid name,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; p1 = Person("Frank")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;calling the instance's name produces the following result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; p1.name
'Frank'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Creating an instance of the Person class with an invalid name and then calling the instance's name produces an error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; p2 = Person(2)
&amp;gt; p2.name
*** AttributeError: 'Person' object has no attribute '_name'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I am pointing out these conditions because you can find many other good examples of how to use class properties, but they will not necessarily work the same way that Example 1 does.  When I was trying to learn about class properties, I tried to follow examples from various blogs but had difficulty getting the class properties to work on my own.  That was because I did not recognize that the examples I found were implementing class properties in slightly different ways than what I was doing.  I did not understand how the differences impacted how the class properties behaved.  With that in mind, here are some important points I learned about creating class properties:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. When the Getter and Setter Functions Are Required
&lt;/h3&gt;

&lt;p&gt;In Example 1, the methods get_name and set_name are required.  If you take out either method and try to set or retrieve a person's name, you will get an error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AttributeError: can't set attribute
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I point this out because there are many blogs which say that each of the default property functions are optional.  While that is technically true, it is not very helpful advice when you are trying to learn how to use a class property to validate an input at the time an instance is created.&lt;/p&gt;

&lt;p&gt;To back up a bit: python's property function contains four default attributes, as described in the python documentation for built-in functions:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;class property(fget=None, fset=None, fdel=None, doc=None)&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Return a property attribute.&lt;/p&gt;

&lt;p&gt;fget is a function for getting an attribute value. fset is a function for setting an attribute value. fdel is a function for deleting an attribute value. And doc creates a docstring for the attribute.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://docs.python.org/3/library/functions.html#property"&gt;https://docs.python.org/3/library/functions.html#property&lt;/a&gt;&lt;/p&gt;


&lt;/blockquote&gt;

&lt;p&gt;There are times when you do not need to use any of the default attributes.  For instance:&lt;/p&gt;

&lt;h4&gt;
  
  
  Example 2:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Dog:

    def __init__(self, name):
        self.name = name

    @property
    def bark(self):
        print("Woof!")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Creating an instance of the Dog class and calling the bark property produces the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; d1 = Dog("Fido")
&amp;gt; d1.bark
Woof!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To use the &lt;code&gt;bark&lt;/code&gt; property of the Dog class instance, there was no need to create a &lt;code&gt;get_bark&lt;/code&gt; or &lt;code&gt;set_bark&lt;/code&gt; function.  However, in order to validate an input as in Example 1, you must create both a getter function and a setter function for the property.  The reason is that instead of &lt;code&gt;name&lt;/code&gt; simply pointing to an assigned value, it points to the &lt;code&gt;property()&lt;/code&gt; function.  Therefore, instead of &lt;code&gt;p1.name&lt;/code&gt; retrieving the assigned value, it calls the property's getter function, which returns the value that has been assigned to &lt;code&gt;_name&lt;/code&gt;.  Similarly, &lt;code&gt;p1.name = 'Tim'&lt;/code&gt; doesn't directly re-assign the &lt;code&gt;name&lt;/code&gt; attribute.  Instead, it calls the property's setter function, which assigns 'Tim' to &lt;code&gt;_name&lt;/code&gt;.  If either the getter or setter function is omitted from Example 1, the class's name property cannot be set or retrieved.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Be Mindful About How Variable Names Are Used
&lt;/h3&gt;

&lt;p&gt;In Example 1,  the variable &lt;code&gt;name&lt;/code&gt; is used in the &lt;code&gt;__init__&lt;/code&gt; function and has the property function assigned to it.  Within get_name and set_name, the variable &lt;code&gt;_name&lt;/code&gt; is used.  You can find many other examples of class properties that use two different variables, with the only difference between the variable names being a leading underscore in one of the variables.  This is a naming convention used to indicate that the variable with the leading underscore is meant to be a "private" attribute.  See a discussion regarding the use of underscores here: &lt;a href="https://stackoverflow.com/questions/1301346/what-is-the-meaning-of-single-and-double-underscore-before-an-object-name"&gt;https://stackoverflow.com/questions/1301346/what-is-the-meaning-of-single-and-double-underscore-before-an-object-name&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By having the variables &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;_name&lt;/code&gt; identical aside from the underscore, it is easier to see that they are related.  Although they are related, they must use different variables.  You cannot use the same variable for both the property assignment and input assignment.  If I change all the attribute values in Example 1 to 'name', it gets stuck in a loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RecursionError: maximum recursion depth exceeded while calling a Python object
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is because the &lt;code&gt;self.name&lt;/code&gt; in the &lt;code&gt;__init__&lt;/code&gt; method calls the &lt;code&gt;property()&lt;/code&gt; function which has been assigned to it.  The property function then invokes either the get_name or set_name function.  If either the get_name or set_name function uses &lt;code&gt;self.name&lt;/code&gt; as the variable, it tries to call the &lt;code&gt;property()&lt;/code&gt; function, which starts the loop over again.  To avoid that issue, the variable used inside get_name and set_name must be distinct.  It is convention to simply modify the property variable by adding an underscrore.&lt;/p&gt;

&lt;p&gt;When learning about class properties, I was confused by many of the examples using the 'underscore' variable in the &lt;code&gt;__init__&lt;/code&gt; method.  For instance, an example in the python documentation for built in functions uses _x as the variable in the &lt;code&gt;__init__&lt;/code&gt;, &lt;code&gt;getx&lt;/code&gt;, &lt;code&gt;setx&lt;/code&gt;, and &lt;code&gt;delx&lt;/code&gt; functions:&lt;/p&gt;

&lt;h4&gt;
  
  
  Example 3
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class C:
    def __init__(self):
        self._x = None

    def getx(self):
        return self._x

    def setx(self, value):
        self._x = value

    def delx(self):
        del self._x

    x = property(getx, setx, delx, "I'm the 'x' property.")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The reason it works to use &lt;code&gt;_x&lt;/code&gt; in all the methods in Example 3 is that the value of _x is not being established at the time an instance of class C is created.  Unlike the conditions for Example 1, where a person's name is provided when a Person instance is created, an instance of class C is created and, only after the instance exists, then x is assigned a value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; c1 = C()
&amp;gt; c1.x
&amp;gt; c1.x = "value"
&amp;gt; c1.x
'value'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The variable &lt;code&gt;_x&lt;/code&gt; is given a default value of &lt;code&gt;None&lt;/code&gt; so that, before &lt;code&gt;x&lt;/code&gt; is assigned a value, &lt;code&gt;c1.x&lt;/code&gt; will return &lt;code&gt;None&lt;/code&gt; rather than causing an AttributeError.&lt;/p&gt;

&lt;p&gt;Example 1 would not work properly if the &lt;code&gt;__init__&lt;/code&gt; method used the &lt;code&gt;_name&lt;/code&gt; variable.  Instead of validating the name at the time an instance is created, it would only validate the name when the name is changed.  A Person instance could be created with a name of the integer 1 (which violates the rule established in the set_name function), but its name could not then be changed to the integer 2 (because a name of 2 does not satisfy the rule in the set_name function):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ipdb&amp;gt; p2 = Person(1)
ipdb&amp;gt; p2.name
1
ipdb&amp;gt; p2.name = 2
ipdb&amp;gt; p2.name
1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But a Person class instance isn't supposed to be created in the first place if the name does not satisfy the rule in the setter function.  So &lt;code&gt;self._name&lt;/code&gt; cannot be used in Example 1's &lt;code&gt;__init__&lt;/code&gt; method.  Instead, it must use &lt;code&gt;self.name&lt;/code&gt;, which points to the set_name property function, so that the name provided at the time an instance is created can be validated.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;There are many blogs and tutorials that explain how to create a class property, and they can be very helpful and informative.  However, in order to get the most out of the advice they offer, it is important to be aware of how an example's use of the property function differs from how you may want to use it.  The values that are provided when a class instance is created and whether those values can be changed make a difference in how the class property methods must be created, and even what default property attributes are required.  Personally, once I understood the underlying reasons for variable names and property attributes, creating class properties became much easier.&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
    </item>
    <item>
      <title>The useState Hook and Forms in Controlled Components</title>
      <dc:creator>Jim Grimes</dc:creator>
      <pubDate>Sun, 15 Oct 2023 20:51:36 +0000</pubDate>
      <link>https://dev.to/jimgrimes86/-the-usestate-hook-and-forms-in-controlled-components-2lj9</link>
      <guid>https://dev.to/jimgrimes86/-the-usestate-hook-and-forms-in-controlled-components-2lj9</guid>
      <description>&lt;p&gt;Hello again!  The past several weeks, my classmates and I have been learning how create websites using React.  For this blog, I will be discussing an example of how to make forms in controlled components in React.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating Forms in Controlled Components
&lt;/h3&gt;

&lt;p&gt;For the purpose of this discussion, I have made a simple app to list and view and search through the pizzas available at my favorite pizzeria:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function App() {
    const [pizzas, setPizzas] = useState([]);

    useEffect(() =&amp;gt; {
        fetch("http://localhost:3001/pizzas")
        .then(resp =&amp;gt; resp.json())
        .then(setPizzas)
    }, [])

    return (
        &amp;lt;div&amp;gt;
            &amp;lt;SearchBar /&amp;gt;
            &amp;lt;AddPizza /&amp;gt;
            &amp;lt;PizzaList pizzas={pizzas} /&amp;gt;
        &amp;lt;/div&amp;gt;
    );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This parent component, App, has three child components:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;SearchBar, which contains a text input form used to search through the available pizzas,&lt;/li&gt;
&lt;li&gt;AddPizza, which contains a form that can be used to add pizzas to the list of available pizzas, and&lt;/li&gt;
&lt;li&gt;PizzaList, which displays the names and descriptions of the pizzas available at the pizzeria.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We are getting information about the available pizzas from a JSON database.  The pizza information will need to be used by each of the child components of App, either to list the pizzas, add a new pizza, or search through the pizzas.  Therefore, the useState hook is used to store the pizza information in App, the closest common ancestor of the three child components.  The pizza information stored in state is passed as a prop to PizzaList, where the names and descriptions of pizzas are displayed.&lt;/p&gt;

&lt;p&gt;The basic elements of AddPizza and SearchBar look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function AddPizza({handleAddPizza}) {

    return (
        &amp;lt;form onSubmit={handleSubmit}&amp;gt;
            &amp;lt;input 
                type="text" 
                name="name" 
                placeholder="Enter Pizza Name" 
            /&amp;gt;
            &amp;lt;input 
                type="text" 
                name="description" 
                placeholder="Enter Pizza Description" 
            /&amp;gt;
            &amp;lt;input type="submit" value="Add Pizza" /&amp;gt;
        &amp;lt;/form&amp;gt;
    )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function SearchBar({handlePizzaSearch}) {

    return (
        &amp;lt;div&amp;gt;
            &amp;lt;input 
                type="text" 
                placeholder="Search Pizzas" 
            /&amp;gt;
        &amp;lt;/div&amp;gt;
    )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both of these components contain text input fields.  We need to plan out: (1) where we are going to store the text a user types into the forms and (2) where the event actions are going to be handled.  In other words, we have to decide how to use state and props in a way that works best with our website.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In React, Controlled Components are those in which form’s data is handled by the component’s state. It takes its current value through props and makes changes through callbacks like onClick, onChange, etc. A parent component manages its own state and passes the new values as props to the controlled component.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;a href="https://www.geeksforgeeks.org/controlled-vs-uncontrolled-components-in-reactjs/"&gt;https://www.geeksforgeeks.org/controlled-vs-uncontrolled-components-in-reactjs/&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As the React documentation notes, we don't always have to make a component completely controlled or completely uncontrolled.  We can customize how state and props are used based on our needs.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In practice, “controlled” and “uncontrolled” aren’t strict technical terms—each component usually has some mix of both local state and props. However, this is a useful way to talk about how components are designed and what capabilities they offer.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;a href="https://react.dev/learn/sharing-state-between-components#controlled-and-uncontrolled-components"&gt;https://react.dev/learn/sharing-state-between-components#controlled-and-uncontrolled-components&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this example, one of the forms will have state stored in the parent component and also receive a callback function as a prop, while the other form will use local state but still receive a callback function as a prop.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Search Bar: Passing Props to Child Component with State in Parent Component
&lt;/h3&gt;

&lt;p&gt;When a user types in the search bar, the list of pizzas will be filtered to find pizza names that match the user's input.  The search does not wait for the user to hit a 'submit' button or any other action to trigger the search.  It filters the list of pizzas as the user types.&lt;/p&gt;

&lt;p&gt;We will store the user's input in state so that the state can be used to in our filter function.  Because the text input in SearchBar must be used in real time to filter the list of pizzas, we should establish our state in App.  Then, since our state is stored in App, we will filter the list of pizzas in App, and pass the filtered list to PizzaList to render the search results.&lt;/p&gt;

&lt;p&gt;With that functionality added, the updated sections of App and SearchBar look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function App() {
    const [pizzas, setPizzas] = useState([]);
    const [searchText, setSearchText] = useState("");

    &amp;lt;!-- useEffect omitted to keep example shorter --&amp;gt;

    function handleAddPizza(newPizza) {
        setPizzas([
        ...pizzas,
        newPizza
        ])
    }

    const filteredPizzas = pizzas.filter(pizza =&amp;gt; pizza.name.toLowerCase().includes(searchText.toLowerCase()))

    return (
        &amp;lt;div&amp;gt;
        &amp;lt;SearchBar handlePizzaSearch={handlePizzaSearch} /&amp;gt;
        &amp;lt;AddPizza handleAddPizza={handleAddPizza} /&amp;gt;
        &amp;lt;PizzaList pizzas={filteredPizzas} /&amp;gt;
        &amp;lt;/div&amp;gt;
    );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function SearchBar({handlePizzaSearch}) {

    function handleSearchChange(event) {
        handlePizzaSearch(event.target.value)
    }

    return (
        &amp;lt;div&amp;gt;
            &amp;lt;input
                type="text"
                placeholder="Search Pizzas"
                onChange={handleSearchChange}
            /&amp;gt;
        &amp;lt;/div&amp;gt;
    )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The user's search input is stored in App as the &lt;code&gt;searchText&lt;/code&gt; state.  When the page loads, &lt;code&gt;searchText&lt;/code&gt; is an empty string.  As the user types, it is changed by &lt;code&gt;setSearchText&lt;/code&gt; to whatever the user types into the search bar.  The &lt;code&gt;.filter()&lt;/code&gt; method is used to filter the list of pizzas, selecting only those pizzas with names that include the text typed into the search bar.  To avoid requiring an exact match of capitalization, the filter changes both the pizza name and the search input to all lower case.  Finally, instead of passing the original &lt;code&gt;pizza&lt;/code&gt; state to the PizzaList component, the list of filtered pizzas is passed.  If &lt;code&gt;searchText&lt;/code&gt; is an empty string, like when the page first loads, every pizza name will be a match so every pizza will be included in the filtered list.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding a Pizza: A Mixture of Local State and Props
&lt;/h3&gt;

&lt;p&gt;The next component, AddPizza, has a form with two text inputs and a submit button.  The text inputs should be updated to reflect what the user types in, but we do not want to try to update or change the pizza database until the user hits the 'submit' button.&lt;/p&gt;

&lt;p&gt;For this component, we can save its state locally, in AddPizza.  We will create a function in App that will update the &lt;code&gt;pizzas&lt;/code&gt; state when the new pizza is submitted.  This function will be passed to AddPizza as a prop.  When the user clicks the 'submit' button, the completed form data will be sent to the database in a POST request.  In turn, the response from the JSON database will be an object containing the new pizza information plus a unique ID number.  We can then pass that object to the callback function from App so that it can be added to &lt;code&gt;pizzas&lt;/code&gt; in App.  The updated, relevant sections of App and AddPizza are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function App() {
    const [pizzas, setPizzas] = useState([]);

    &amp;lt;!-- state for searchText and useEffect omitted for brevity --&amp;gt;

    function handleAddPizza(newPizza) {
        setPizzas([
            ...pizzas,
            newPizza
        ])
    }

    &amp;lt;!-- functions for handlePizzaSearch and filteredPizzas omitted --&amp;gt;

    return (
        &amp;lt;div&amp;gt;
            &amp;lt;SearchBar handlePizzaSearch={handlePizzaSearch} /&amp;gt;
            &amp;lt;AddPizza handleAddPizza={handleAddPizza} /&amp;gt;
            &amp;lt;PizzaList pizzas={filteredPizzas} /&amp;gt;
        &amp;lt;/div&amp;gt;
    );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function AddPizza({handleAddPizza}) {
    const [formData, setFormData] = useState({
        name: "",
        description: ""
    })

    function handleFormChange(event) {
        setFormData({
            ...formData,
            [event.target.name]: event.target.value
        })
    }

    function handleSubmit(event) {
        event.preventDefault();
        fetch("http://localhost:3001/pizzas", {
            method: "POST",
            headers: {
                "Content-Type": "application/json"
            },
            body: JSON.stringify(formData)
        })
        .then(resp =&amp;gt; resp.json())
        .then(newPizza =&amp;gt; handleAddPizza(newPizza))
    }

    return (
        &amp;lt;form onSubmit={handleSubmit}&amp;gt;
            &amp;lt;input 
                type="text" 
                name="name" 
                placeholder="Enter Pizza Name" 
                value={formData.name} 
                onChange={handleFormChange} 
            /&amp;gt;
            &amp;lt;input 
                type="text" 
                name="description" 
                placeholder="Enter Pizza Description" 
                value={formData.description} 
                onChange={handleFormChange} 
            /&amp;gt;
            &amp;lt;input type="submit" value="Add Pizza" /&amp;gt;
        &amp;lt;/form&amp;gt;
    )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both form text input fields begin with default values of empty strings.  As the user types in the form, the name and value of the form field in which they type are used to update the section of formData that corresponds to the form field.  The values in the formData object are used as the values shown in the form's text input so that the text the user sees matches the information stored in state.  When a value in formData is an empty string, such as when the page first loads, the placeholder text is visible in the text input field.&lt;/p&gt;

&lt;p&gt;After the new pizza is added to the database, we are using the response from JSON POST request to update the web page's list of pizzas, instead of using the information stored in state as formData.  That is so the pizza added to &lt;code&gt;pizzas&lt;/code&gt; will include a unique ID number which can be used as the key for the JSX list.&lt;/p&gt;

&lt;p&gt;Finally, when we update &lt;code&gt;pizzas&lt;/code&gt; in App to include the new pizza, we do so by making a copy of the current array stored in &lt;code&gt;pizzas&lt;/code&gt;, not by simply adding the new pizza to the current array.  By making a copy, React recognizes that we have changed the state and re-renders the page in order to reflect the new state.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Planning out how and where to use state is an important part of working with React components that include forms.  Approaches similar to the ones outlined above can be used with toggle buttons, drop-down menus, and other features that require you to store input values in state and update the information on a page based changes to state.  Thanks for reading!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Using Fetch Requests to Update a JSON Database Without Creating Duplicate Database Entries</title>
      <dc:creator>Jim Grimes</dc:creator>
      <pubDate>Sun, 01 Oct 2023 20:31:44 +0000</pubDate>
      <link>https://dev.to/jimgrimes86/using-fetch-requests-to-update-a-json-database-without-creating-duplicate-database-entries-4l8a</link>
      <guid>https://dev.to/jimgrimes86/using-fetch-requests-to-update-a-json-database-without-creating-duplicate-database-entries-4l8a</guid>
      <description>&lt;p&gt;Hello!  In this blog, we will work through a process of checking whether information already exists in a JSON database and, if it doesn't, adding the information to the database.&lt;/p&gt;

&lt;h3&gt;
  
  
  Starting Conditions
&lt;/h3&gt;

&lt;p&gt;As an example, we will use a JSON database containing basic information about pets.  Each database entry includes the pet's name, age, and what kind of animal it is.  When a user provides information about a pet, our task is to add the pet to the database.  However, we do not want to create duplicate database entries, so we only want to add the user's input to the database if it does not already exist somewhere in the database.  We start with the following database of pets:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
"pets": [
    {
    "id": 1,
    "name": "Teddy",
    "animal": "dog",
    "age": 5
    },
    {
    "id": 2,
    "name": "Biscuit",
    "animal": "cat",
    "age": 8
    },
    {
    "id": 3,
    "name": "Bubbles",
    "animal": "goldfish",
    "age": 1
    }
]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The pet information from the user is provided in the following object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let petInput = {
    name: "Scout",
    animal: "dog",
    age: "3"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Checking and Updating the Database
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Retrieving Database Information
&lt;/h4&gt;

&lt;p&gt;In order to compare the user's input with the objects that already exist in the database, we first need to obtain the current database information.  We do this by using fetch() to send a GET request to the JSON database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getDatabaseInfo(petInput) {
    fetch('http://localhost:3000/pets')
    .then(response =&amp;gt; response.json())
    .then(databaseInfo =&amp;gt; databaseCheckFunction(databaseInfo, petInput))
}
getDatabaseInfo(petInput)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function, getDatabaseInfo, is being used to start a chain of synchronous functions.  Therefore, the user's input, petInput, is provided as an argument so that it can be passed as an argument as callback functions are invoked.&lt;/p&gt;

&lt;p&gt;If you want to review the use of the fetch method and how to handle response received from the database, you can visit this site: &lt;a href="https://javascript.info/fetch"&gt;https://javascript.info/fetch&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Searching for Matching Database Entries
&lt;/h4&gt;

&lt;p&gt;Now that we have an array of the current database objects, we can iterate through the array, searching for entries that match the user's input.  For instance, we can use the find() method to search for objects with matching pet names:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function databaseCheckFunction(databaseInfo, petInput) {
    let searchedItem = databaseInfo.find(databaseEntry =&amp;gt; {
        if (databaseEntry.name == petInput.name) {
            return true}
        })
    if (searchedItem == undefined) {
        addNewPetToDatabase(petInput)
    } else {
        console.log('This pet name already exists in the database')
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;.find() will return the matching database entry if a match is found.  If a matching entry is not found, it will return undefined.  We want to add the new pet to the database only if a matching entry is not found, i.e. if .find() returns undefined.  &lt;/p&gt;

&lt;p&gt;For more information about using .find(), see the documentation here: &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Adding New Pet to the Database
&lt;/h4&gt;

&lt;p&gt;If no matching name was found in the database, the following function, addNewPetToDatabase, will be invoked.  This function uses fetch() to send a POST request to add the new pet to database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function addNewPetToDatabase(petInput) {
    fetch('http://localhost:3000/pets', {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            "Accept": "application/json"
        },
        body: JSON.stringify(petInput)
    })
    .then(response =&amp;gt; response.json())
    .then(newDatabaseEntry =&amp;gt; console.log(newDatabaseEntry))
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the POST request, we can console log the response to confirm that the pet was successfully added to the database and see the database id number assigned to the pet.&lt;/p&gt;

&lt;h4&gt;
  
  
  Updating Existing Database Entries
&lt;/h4&gt;

&lt;p&gt;In addition to adding a new pet to the database if a matching object value is not found, we can use the same method to update a database entry if a matching object is found.  Instead of having our databaseCheckFunction console log a message if a matching pet name is found, we can pass the petInput object to a function that will use a PATCH or PUT request to update the database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function databaseCheckFunction(databaseInfo, petInput) {
    let searchedItem = databaseInfo.find(databaseEntry =&amp;gt; {
        if (databaseEntry.name == petInput.name) {
            return true}
        })
    // .find() will return the matching entry if a match is found.  Otherwise, it will return undefined.  We want to add the new pet to the database if a matching entry is not returned.
    if (searchedItem == undefined) {
        addNewPetToDatabase(petInput)
    } else {
        updatePetInDatabase(petInput)
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we will use a PUT request to overwrite an existing database entry with the petInput information:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function updatePetInDatabase(foundItem, petInput) {
    fetch('http://localhost:3000/pets/'+foundItem.id, {
        method: "PUT",
        headers: {
            "Content-Type": "application/json",
            "Accept": "application/json"
        },
        body: JSON.stringify(petInput)
    })
    .then(response =&amp;gt; response.json())
    .then(updatedDatabaseEntry =&amp;gt; console.log(updatedDatabaseEntry))
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The following blog discusses the difference between PATCH and PUT requests: &lt;a href="https://medium.com/@9cv9official/what-are-get-post-put-patch-delete-a-walkthrough-with-javascripts-fetch-api-17be31755d28"&gt;https://medium.com/@9cv9official/what-are-get-post-put-patch-delete-a-walkthrough-with-javascripts-fetch-api-17be31755d28&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;If we are unsure whether information already exists in a database, we retrieve the database with fetch using a GET request and then iterate through the response, searching for matching information.  In this example, we searched for a matching name using the .find() method, but other methods can be used.  For instance, if we need to anticipate finding several instances of matching database entries, we could use the .filter() method.  If no match is found, the new information can be added to the database with a POST request.  Alternatively, an existing database entry can be altered by using a PATCH or PUT request.&lt;/p&gt;

&lt;p&gt;I have found the above-outlined steps to be useful in completing multiple tasks assigned to me as a software engineering student, and I hope they can be useful to you as well. Thanks for reading!&lt;/p&gt;

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