I am trying to better understand SQL Alchemy to start using this ORM with Flask. I have two SQL tables. There is a many - one relationship between the two tables. I am trying to find a way by using SQL Alchemy to add an entry to the child table by using a foreing key but using a different field in the parent table.
│ .dockerignore
│ .env
│ .env-template
│ .gitignore
│ docker-compose.yml
│ Dockerfile
│ manage.py
│ README.md
│ requirements.txt
├───app
│ │ config.py
│ │ routes.py
│ │ __init__.py
│ │
│ ├───sites
│ │ controller.py
│ │ interface.py
│ │ model.py
│ │ schema.py
│ │ service.py
│ │ __init__.py
│ │
│ └───devices
│ controller.py
│ interface.py
│ model.py
│ schema.py
│ service.py
│ __init__.py
├───config
│ gunicorn.py
app --> sites --> model.py
from sqlalchemy import Integer, Column, String
from app import db # noqa
from .interface import SiteInterface
class Site(db.Model): # type: ignore
"""Site"""
__tablename__ = "sites"
site_db_id = Column(Integer(), primary_key=True, autoincrement=True, nullable=False)
dc_site_id = Column(Integer(), unique=True)
dc_name = Column(String(100))
app --> sites --> interface.py
from mypy_extensions import TypedDict
class SiteInterface(TypedDict, total=False):
site_id: int
dc_site_id: int
dc_name: str
app --> sites --> schema.py
from marshmallow import fields, Schema
class SiteSchema(Schema):
"""Site schema"""
siteId = fields.Number(attribute="site_db_id")
dcsiteId = fields.Number(attribute="dc_site_id")
dcname = fields.String(attribute="dc_name")
app --> devices --> model.py
from sqlalchemy import Integer, Column, String, ForeignKey
from sqlalchemy.orm import relationship
from app import db # noqa
from .interface import DeviceInterface
class Device(db.Model): # type: ignore
"""Device"""
__tablename__ = "devices"
device_id = Column(Integer(), primary_key=True, autoincrement=True, nullable=False)
hostname = Column(String(50), nullable=False)
ip = Column(String(15), unique=True)
site_db_id = Column(Integer, ForeignKey('sites.site_db_id'))
sites = relationship("Site")
app --> devices --> interface.py
from mypy_extensions import TypedDict
class DeviceInterface(TypedDict, total=False):
deviceId: int
hostname: str
ip: str
sitedbId: int
app --> devices --> schema.py
from marshmallow import fields, Schema
class DeviceSchema(Schema):
"""Device schema"""
deviceId = fields.Number(attribute="device_id")
hostname = fields.String(attribute="hostname")
ip = fields.String(attribute="ip")
sitedbId = fields.Number(attribute="site_db_id")
I need help to find the way to be able to add a new Device and specify the site_db_id
using the dc_site_id
Something like this.
@staticmethod
def create(new_attrs: DeviceInterface) -> Device:
new_device = Device(
hostname=new_attrs["hostname"], ip=new_attrs["ip"],
site_db_id=new_attrs["????"] *** select site_db_id FROM sites where dc_site_id=site_db_id ****
)
db.session.add(new_device)
db.session.commit()
return new_device
Thank you in advance for helping!!!
If I understand your requirement, you can use a two step approach: First select the Site using dc_site_id, then create the new Device with the Site found. Code could look for example like:
dc_site_id = new_attrs["dc_site_id "]
sitefound = Site.query.filter_by(dc_site_id = dc_site_id ).first_or_404()
new_device = Device(...,...., relatedsite = sitefound)
where relatedsite is the backref that you add in the Site class for your relationship.
devices= db.relationship('Device', backref='relatedsite', lazy=True)
This was this approach for one to many relationships that I found initially in the flask-sqlalchemy manual :
class Person(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
addresses = db.relationship('Address', backref='person', lazy=True)
class Address(db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(120), nullable=False)
person_id = db.Column(db.Integer, db.ForeignKey('person.id'),
nullable=False)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.