-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed.py
More file actions
37 lines (28 loc) · 1.04 KB
/
seed.py
File metadata and controls
37 lines (28 loc) · 1.04 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
import json
from sqlmodel import Session, create_engine, SQLModel
from app.models import Character
import os
from dotenv import load_dotenv
load_dotenv()
DATABASE_URL = os.getenv("DATABASE_URL")
engine = create_engine(DATABASE_URL)
def seed_data():
SQLModel.metadata.create_all(engine)
with open("/home/hisham/code/Hishamkhashman1/rick-and-morty-API/data/characters.json", "r") as f:
json_data = json.load(f)
with Session(engine) as session:
for character in json_data:
character = Character(
id = character["id"],
name = character["name"],
status = character["status"],
species = character["species"],
type = character["type"],
gender = character["gender"],
place_of_origin = character["place of origin"]
)
session.add(character)
session.commit()
print("Data moved to Postgres successfully")
if __name__ == "__main__":
seed_data()