-
Notifications
You must be signed in to change notification settings - Fork 3
Tutorial: Adding sample data to database
###Creating Database and Sample Data
-
From your terminal, launch the python terminal.
python
Once you are in the python terminal, the command prompt should be >>>
.
-
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
-
Create an instance of a model. For example, create a FoodResource object called
fr1
.>>> fr1 = FoodResource()
-
Populate the database fields pertaining to the model. For example, populate
fr1
'sname
anddescription
fields. Thename
anddescription
fields correspond to columns in theFoodResource
model. Check outmodels.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"
-
To add an object to the database, you must first
add
and thencommit
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
-
Many of our models have at least one one-to-one or one-to-many relationship with another model. For example, each
FoodResource
has anAddress
. 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()
-
Now, add
a1
tofr1
.>>> 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()
-
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'
-
And that's it! You have now created a
FoodResource
and anAddress
. Seepopulate.py
for how to create more comprehensive sample data.
###Additional Resources