aurweb/aurweb/models/package_keyword.py
Kevin Morris 888cf5118a use declarative_base for all ORM models
This rewrites the entire model base as declarative models.
This allows us to more easily customize overlay fields
in tables and is more common.

This effort also brought some DB violations to light which
this commit addresses.

Signed-off-by: Kevin Morris <kevr@0cost.org>
2021-06-10 13:54:27 -07:00

33 lines
1 KiB
Python

from sqlalchemy import Column, ForeignKey, Integer
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import backref, relationship
import aurweb.db
import aurweb.models.package_base
from aurweb.models.declarative import Base
class PackageKeyword(Base):
__tablename__ = "PackageKeywords"
PackageBaseID = Column(
Integer, ForeignKey("PackageBases.ID", ondelete="CASCADE"),
primary_key=True, nullable=True)
PackageBase = relationship(
"PackageBase", backref=backref("keywords", lazy="dynamic"),
foreign_keys=[PackageBaseID])
__mapper_args__ = {"primary_key": [PackageBaseID]}
def __init__(self,
PackageBase: aurweb.models.package_base.PackageBase = None,
Keyword: str = None):
self.PackageBase = PackageBase
if not self.PackageBase:
raise IntegrityError(
statement="Primary key PackageBaseID cannot be null.",
orig="PackageKeywords.PackageBaseID",
params=("NULL"))
self.Keyword = Keyword