Skip to content

Commit

Permalink
not working -error with db
Browse files Browse the repository at this point in the history
  • Loading branch information
LiamBrem committed Sep 28, 2023
1 parent e40e42a commit 748ca4d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
How to migrate db after making changes to the models:

flask db migrate -m "message"
flask db upgrade
7 changes: 5 additions & 2 deletions models/parent.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from .user import UserModel
from .student import StudentModel
from app import db

class ParentModel(UserModel):
__tablename__ = 'parents'
def __init__(self, id, username, email, date_of_birth, password):
super().__init__(id, username, email, date_of_birth, password)
children = db.relationship('StudentModel', back_populates='parent', foreign_keys=[StudentModel.parent_id])

def __init__(self, id, firstname, lastname, email, date_of_birth, password):
super().__init__(id, firstname, lastname, email, date_of_birth, password)
7 changes: 4 additions & 3 deletions models/student.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from .user import UserModel
from app import db
#import mappend_column
from sqlalchemy.orm import mapped_column

from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship

class StudentModel(UserModel):
__tablename__ = 'students'
#parents is the name of the table
parent_id = mapped_column(db.ForeignKey('parents.id'))
parent_id = db.Column(db.String(16), db.ForeignKey('parents.id'), nullable=True)
parent = db.relationship('ParentModel', back_populates='children')
grade = db.Column(db.Integer, nullable=True)

Expand Down
2 changes: 1 addition & 1 deletion models/user.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from app import db

class UserModel(db.Model):
id = db.Column(db.String(80), unique=True, primary_key=True)
id = db.Column(db.String(16), unique=True, primary_key=True)
firstname = db.Column(db.String(80), unique=False, nullable=False)
lastname = db.Column(db.String(80), unique=False, nullable=False)
email = db.Column(db.String(80), unique=False, nullable=False)
Expand Down
14 changes: 12 additions & 2 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,22 @@ def checkHttp():
"role": "student",
"date_of_birth": "2000-01-01",
"password": "securepassword",
"parent_id": "1234567890123456",
"grade": "10",
}

response = requests.post(f'{BASE_URL}/user', json=student_data)
parent_data = {
"firstname": "John",
"lastname": "Doe",
"email": "johndoe@example.com",
"role": "parent",
"date_of_birth": "2000-01-01",
"password": "securepassword",
}

response = requests.post(f'{BASE_URL}/user', json=parent_data)
if response.status_code == 201:
print("Student user created successfully!")
print("Parent user created successfully!")
print(response.json())
else:
print("Error creating student user:")
Expand Down
5 changes: 4 additions & 1 deletion views/userViews.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ def create_user():

if not id or not firstname or not lastname or not email:
return jsonify({"message": "Missing required fields"}), 400

print("ROLE:", role)

if role == "teacher":
# Teachers require an additional 'subject' parameter
Expand All @@ -74,7 +76,7 @@ def create_user():
elif role == "student":
# Students require an additional 'grade_level' parameter
grade = data.get("grade")
print(grade)
parent_id = data.get("parent_id")

if not grade:
return jsonify({"message": "Missing 'grade' field for student"}), 400
Expand All @@ -86,6 +88,7 @@ def create_user():
email=email,
date_of_birth=date_of_birth,
password=password,
parent_id=parent_id,
grade=grade,
)
elif role == "parent":
Expand Down

0 comments on commit 748ca4d

Please sign in to comment.