-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseeds.py
More file actions
73 lines (53 loc) · 3.36 KB
/
seeds.py
File metadata and controls
73 lines (53 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from app.models import User
from app.db import Session, Base, engine
from app.models import User, Post, Comment, Vote
# drop and rebuild tables
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
db = Session()
# insert users
db.add_all([
User(username='alesmonde0', email='nwestnedge0@cbc.ca', password='password123'),
User(username='jwilloughway1', email='rmebes1@sogou.com', password='password123'),
User(username='iboddam2', email='cstoneman2@last.fm', password='password123'),
User(username='dstanmer3', email='ihellier3@goo.ne.jp', password='password123'),
User(username='djiri4', email='gmidgley4@weather.com', password='password123')
])
db.commit()
# insert posts
db.add_all([
Post(title='Donec posuere metus vitae ipsum', post_url='https://buzzfeed.com/in/imperdiet/et/commodo/vulputate.png', user_id=1),
Post(title='Morbi non quam nec dui luctus rutrum', post_url='https://nasa.gov/donec.json', user_id=1),
Post(title='Donec diam neque, vestibulum eget, vulputate ut, ultrices vel, augue', post_url='https://europa.eu/parturient/montes/nascetur/ridiculus/mus/etiam/vel.aspx', user_id=2),
Post(title='Nunc purus', post_url='http://desdev.cn/enim/blandit/mi.jpg', user_id=3),
Post(title='Pellentesque eget nunc', post_url='http://google.ca/nam/nulla/integer.aspx', user_id=4)
])
db.commit()
# insert comments
db.add_all([
Comment(comment_text='Nunc rhoncus dui vel sem.', user_id=1, post_id=2),
Comment(comment_text='Morbi odio odio, elementum eu, interdum eu, tincidunt in, leo. Maecenas pulvinar lobortis est.', user_id=1, post_id=3),
Comment(comment_text='Aliquam erat volutpat. In congue.', user_id=2, post_id=1),
Comment(comment_text='Quisque arcu libero, rutrum ac, lobortis vel, dapibus at, diam.', user_id=2, post_id=3),
Comment(comment_text='In hac habitasse platea dictumst.', user_id=3, post_id=3)
])
db.commit()
# insert votes
db.add_all([
Vote(user_id=1, post_id=2),
Vote(user_id=1, post_id=4),
Vote(user_id=2, post_id=4),
Vote(user_id=3, post_id=4),
Vote(user_id=4, post_id=2)
])
db.commit()
db.close()
# This is where the db variables that you created earlier come into play.
# The code uses the Base class together with the engine connection variable to do two things.
# First, it drops all the existing tables. Second, it creates any tables that Base mapped, in a class that inherits Base (like User).
# Use the following command to run the script: python seeds.py
# Anytime we want to perform a CRUD operation using SQLAlchemy, we need to establish a temporary session connection with the Session class. Within this db session object, we use the add_all() method and the User model to create several new users.
# Note that db.add_all() alone doesn't change the database. In fact, it only preps the SQL queries. To run the INSERT statements, you need to call db.commit(). If you don't need to make any other database transactions at this time, you can close the session connection by calling db.close().
# Run the script again using the command python seeds.py, and note the output. The command line should print something like the following:
# 2020-08-07 11:24:57,709 INFO sqlalchemy.engine.base.Engine INSERT INTO users (username, email, password) VALUES (%(username)s, %(email)s, %(password)s)
# 2020-08-07 11:24:57,710 INFO sqlalchemy.engine.base.Engine {'username': 'alesmonde0', 'email': 'nwestnedge0@cbc.ca', 'password': 'password123'}