Skip to content

Tutorial: Adding sample data to database

weronica edited this page Dec 27, 2014 · 8 revisions

###Creating Database and Sample Data

  1. From your terminal, launch the python terminal.

     python
    

Once you are in the python terminal, the command prompt should be >>>.

  1. Run the following commands in the python terminal to set up your database. (Anything after a # is a comment and can thus be ignored.)

     >>> from app import db
     >>> from app.models import * # imports all models 
     >>> db.create_all() # actually create the database
    
  2. Create an instance of a model. For example, create a FoodResource object called fr1.

     >>> fr1 = FoodResource()
    
  3. Populate the database fields pertaining to the model. For example, populate fr1's name and description fields. The name and description fields correspond to columns in the FoodResource model. Check out models.py to see all of the fields for our models.

     >>> fr1.name = "South Philadelphia Farmers' Market"
     >>> fr1.description = "A local farmers' market that sells fresh fruit and vegetables." 
     >>> fr1.location_type = "FARMERS MARKET" # must match an existing enum
    

You can confirm that the fields have been correctly set by simply calling fr1.name and fr1.description in the terminal. If you're familiar with object-oriented programming (OOP), these act as both setters (or accessors) and getters (or mutators).

    >>> fr1.name
    "South Philadelphia Farmers' Market"
    >>> fr1.description
    "A local farmers' market that sells fresh fruit and vegetables."
    >>> fr1.location_type
    "FARMERS_MARKET"
  1. To add an object to the database, you must first add and then commit it. For example,

     >>> db.session.add(fr1)
     >>> db.session.commit()
    

Now, if you query the database for all of the FoodResource objects, you should see that there is one FoodResource object in the database.

    >>> FoodResource.query.all()
    <app.models.FoodResource object at 0x10f4d45d0>
    >>> len(FoodResource.query.all()) # query for total number of FoodResources
    1
  1. Many of our models have at least one one-to-one or one-to-many relationship with another model. For example, each FoodResource has an Address. In order to create an Address for a fr1, you must for create an Address object:

     >>> a1 = Address()
     >>> a1.line1 = "43rd Street and Baltimore Avenue"
     >>> a1.city = "Philadelphia"
     >>> a1.state = "PA"
     >>> a1.zip_code = "19104"
    

Add a1 to the database.

    >>> db.session.add(a1)
    >>> db.session.commit()
  1. Now, add a1 to fr1.

     >>> fr1.address = a1
    

Anytime that you add or update an object model, you must add and commit again. This includes updating a one-to-one or one-to-many relationship. For example, if you update fr1.address after fr1 has been add'ed and commit'ed (e.g., fr1.address = a1 (another Address that you created)), then you must add and commit fr1 again.

    >>> db.session.add(fr1)
    >>> db.session.commit()
  1. Here are a few other useful database queries:

     >>> FoodResource.query.first()
     <app.models.FoodResource object at 0x10f4d4c10>
     >>> FoodResource.query.first().name
     u"South Philadelphia Farmers' Market"
     >>> FoodResource.query.first().address
     <app.models.Address object at 0x10f4dd710>
     >>> FoodResource.query.first().address.line1
     u'43rd Street and Baltimore Avenue'
    
  2. And that's it! You have now created a FoodResource and an Address. See populate.py for how to create more comprehensive sample data.

###Additional Resources

Clone this wiki locally