Fix some eager loading nonsense.
This commit is contained in:
parent
b39384f358
commit
39a8199618
3 changed files with 51 additions and 32 deletions
|
|
@ -1,30 +1,54 @@
|
||||||
from typing import TYPE_CHECKING
|
# inventory/models/__init__.py
|
||||||
|
from inventory import db # your single SQLAlchemy() instance
|
||||||
|
|
||||||
from inventory import db # Yes, this works when run from project root with Alembic
|
# Import *modules* so all model classes are defined & registered
|
||||||
|
from . import image
|
||||||
|
from . import room_functions
|
||||||
|
from . import rooms
|
||||||
|
from . import areas
|
||||||
|
from . import brands
|
||||||
|
from . import items
|
||||||
|
from . import inventory
|
||||||
|
from . import work_log
|
||||||
|
from . import work_note
|
||||||
|
from . import users
|
||||||
|
from . import image_links
|
||||||
|
|
||||||
from .areas import Area
|
# If you want convenient symbols, export them AFTER modules are imported
|
||||||
from .brands import Brand
|
Image = image.Image
|
||||||
from .items import Item
|
ImageAttachable = image.ImageAttachable
|
||||||
from .inventory import Inventory
|
RoomFunction = room_functions.RoomFunction
|
||||||
from .room_functions import RoomFunction
|
Room = rooms.Room
|
||||||
from .users import User
|
Area = areas.Area
|
||||||
from .work_log import WorkLog
|
Brand = brands.Brand
|
||||||
from .rooms import Room
|
Item = items.Item
|
||||||
from .work_note import WorkNote
|
Inventory = inventory.Inventory
|
||||||
from .image import Image
|
WorkLog = work_log.WorkLog
|
||||||
from .image_links import worklog_images
|
WorkNote = work_note.WorkNote
|
||||||
|
worklog_images = image_links.worklog_images
|
||||||
|
User = users.User
|
||||||
|
|
||||||
|
# Now it’s safe to configure mappers and set global eagerloads
|
||||||
|
from sqlalchemy.orm import configure_mappers, joinedload, selectinload
|
||||||
|
configure_mappers()
|
||||||
|
|
||||||
|
User.ui_eagerload = (
|
||||||
|
joinedload(User.supervisor),
|
||||||
|
joinedload(User.location).joinedload(Room.room_function),
|
||||||
|
)
|
||||||
|
|
||||||
|
Room.ui_eagerload = (
|
||||||
|
joinedload(Room.area),
|
||||||
|
joinedload(Room.room_function),
|
||||||
|
selectinload(Room.inventory),
|
||||||
|
selectinload(Room.users)
|
||||||
|
)
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"db",
|
"db",
|
||||||
"Area",
|
"Image", "ImageAttachable",
|
||||||
"Brand",
|
"RoomFunction", "Room",
|
||||||
"Item",
|
"Area", "Brand", "Item", "Inventory",
|
||||||
"Inventory",
|
"WorkLog", "WorkNote", "worklog_images",
|
||||||
"RoomFunction",
|
|
||||||
"User",
|
"User",
|
||||||
"WorkLog",
|
|
||||||
"Room",
|
|
||||||
"WorkNote",
|
|
||||||
"Image",
|
|
||||||
"worklog_images"
|
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ if TYPE_CHECKING:
|
||||||
from .users import User
|
from .users import User
|
||||||
|
|
||||||
from sqlalchemy import ForeignKey, Identity, Integer, Unicode
|
from sqlalchemy import ForeignKey, Identity, Integer, Unicode
|
||||||
from sqlalchemy.orm import Mapped, mapped_column, relationship, joinedload, selectinload
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||||
|
|
||||||
from . import db
|
from . import db
|
||||||
|
|
||||||
|
|
@ -52,10 +52,3 @@ class Room(db.Model):
|
||||||
name = self.name or ""
|
name = self.name or ""
|
||||||
func = self.room_function.description if self.room_function else ""
|
func = self.room_function.description if self.room_function else ""
|
||||||
return f"{name} - {func}".strip(" -")
|
return f"{name} - {func}".strip(" -")
|
||||||
|
|
||||||
Room.ui_eagerload = (
|
|
||||||
joinedload(Room.area),
|
|
||||||
joinedload(Room.room_function),
|
|
||||||
selectinload(Room.inventory),
|
|
||||||
selectinload(Room.users)
|
|
||||||
)
|
|
||||||
|
|
@ -31,6 +31,8 @@ class User(db.Model, ImageAttachable):
|
||||||
inventory: Mapped[List['Inventory']] = relationship('Inventory', back_populates='owner')
|
inventory: Mapped[List['Inventory']] = relationship('Inventory', back_populates='owner')
|
||||||
image: Mapped[Optional['Image']] = relationship('Image', back_populates='user', passive_deletes=True)
|
image: Mapped[Optional['Image']] = relationship('Image', back_populates='user', passive_deletes=True)
|
||||||
|
|
||||||
|
ui_eagerload = tuple()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def identifier(self) -> str:
|
def identifier(self) -> str:
|
||||||
return f"{self.first_name or ''} {self.last_name or ''}{', ' + (''.join(word[0].upper() for word in self.title.split())) if self.title else ''}".strip()
|
return f"{self.first_name or ''} {self.last_name or ''}{', ' + (''.join(word[0].upper() for word in self.title.split())) if self.title else ''}".strip()
|
||||||
|
|
@ -73,4 +75,4 @@ class User(db.Model, ImageAttachable):
|
||||||
title=data.get("title"),
|
title=data.get("title"),
|
||||||
location_id=data.get("location_id"),
|
location_id=data.get("location_id"),
|
||||||
supervisor_id=data.get("supervisor_id")
|
supervisor_id=data.get("supervisor_id")
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue