Exemplo n.º 1
0
# Setup up the database

from BasicModelApp import db, Puppy  # Import db object and Puppy class

db.create_all()  # Create the tables in the database

sam = Puppy('Sammy', 3)  # Create new entries in the database
frank = Puppy('Frankie', 4)

print(sam.id)  # Check ids (Not added to the database so should be None)
print(frank.id)

db.session.add_all([
    sam, frank
])  # Ids will get created automatically once we add these entries to the DB

# db.session.add(sam)                   # Alternative for individual additions
# db.session.add(frank)

db.session.commit()  # Now save it to the database

print(sam.id)  # Check the ids
print(frank.id)
# This is a very simple script that will show you how to setup our DB
# Later on we'll want to use this type of code with templates

#############################################################################
### NOTE!! If you run this script multiple times you will add ##############
### multiple puppies to the database. That is okay, just the ##############
### ids will be higher than 1, 2 on the subsequent runs ##################
#########################################################################

# Import database info
from BasicModelApp import db, Puppy

# Create the tables in the database
# (Usually won't do it this way!)
# this should not be in script but in command line
db.create_all()

# Create new entries in the database
sam = Puppy('Sammy', 3)
frank = Puppy('Frankie', 4)

# Check ids (haven't added sam and frank to database, so they should be None)
print(sam.id)
print(frank.id)

# Ids will get created automatically once we add these entries to the DB
db.session.add_all([sam, frank])

# Alternative for individual additions:
# db.session.add(sam)
# db.session.add(frank)