mirror of
https://github.com/mariadb-corporation/dev-example-blog-samples.git
synced 2025-08-01 15:54:56 +00:00
SQLAlchemy part 2 updates
This commit is contained in:
@ -10,7 +10,7 @@ class ContactDetails(Base):
|
||||
phone_number = Column(String(length=12))
|
||||
address = Column(String(length=100))
|
||||
employee_id = Column(Integer, ForeignKey('employees.id'))
|
||||
employee = relationship("Employee", backref=backref("contact_details", uselist=False))
|
||||
employee = relationship("Employee", back_populates="contact_details")
|
||||
|
||||
def __init__(self, phone_number, address, employee):
|
||||
self.phone_number = phone_number
|
||||
|
@ -12,8 +12,7 @@ class Employee(Base):
|
||||
lastname = Column(String(length=100))
|
||||
active = Column(Boolean, default=True)
|
||||
department = relationship("Department", back_populates="employees")
|
||||
|
||||
#contact_details = relationship("ContactDetails", backref=backref("contact_details", uselist=False))
|
||||
contact_details = relationship("ContactDetails", uselist=False, back_populates="employee")
|
||||
|
||||
def __init__(self, firstname, lastname, department):
|
||||
self.firstname = firstname
|
||||
|
@ -34,11 +34,11 @@ project_2.employees = [emp_jack,emp_ben,emp_sun]
|
||||
project_3.employees = [emp_john,emp_kate,emp_jack,emp_ben,emp_sun]
|
||||
|
||||
# Create contact details
|
||||
cd_john = ContactDetails("417 315 2531", "123 S Main ST", emp_john)
|
||||
cd_kate = ContactDetails("417 315 2533", "124 S Main ST", emp_kate)
|
||||
cd_jack = ContactDetails("417 315 2534", "125 S Main ST", emp_jack)
|
||||
cd_ben = ContactDetails("417 315 2535", "126 S Main ST", emp_ben)
|
||||
cd_sun = ContactDetails("417 315 2536", "127 S Main ST", emp_sun)
|
||||
cd_john = ContactDetails("417-315-2531", "123 S Main ST", emp_john)
|
||||
cd_kate = ContactDetails("816-315-2533", "124 S Main ST", emp_kate)
|
||||
cd_jack = ContactDetails("773-315-2534", "125 S Main ST", emp_jack)
|
||||
cd_ben = ContactDetails("518-315-2535", "126 S Main ST", emp_ben)
|
||||
cd_sun = ContactDetails("913-315-2536", "127 S Main ST", emp_sun)
|
||||
|
||||
|
||||
# Persist data
|
||||
|
@ -2,9 +2,18 @@ from base import Session
|
||||
from project import Project
|
||||
from employee import Employee
|
||||
from department import Department
|
||||
from contact_details import ContactDetails
|
||||
|
||||
session = Session()
|
||||
|
||||
# Get all employees
|
||||
|
||||
employees = session.query(Employee).all()
|
||||
|
||||
print('### Employees ###')
|
||||
for employee in employees:
|
||||
print(f' - {employee.firstname} {employee.lastname}, phone: {employee.contact_details.phone_number}')
|
||||
|
||||
# Get all projects
|
||||
projects = session.query(Project).all()
|
||||
|
||||
|
Reference in New Issue
Block a user