fix(auth): Fix DetachedInstanceError on dashboard for users with company associations
Some checks are pending
NordaBiz Tests / Unit & Integration Tests (push) Waiting to run
NordaBiz Tests / E2E Tests (Playwright) (push) Blocked by required conditions
NordaBiz Tests / Smoke Tests (Production) (push) Blocked by required conditions
NordaBiz Tests / Send Failure Notification (push) Blocked by required conditions
Some checks are pending
NordaBiz Tests / Unit & Integration Tests (push) Waiting to run
NordaBiz Tests / E2E Tests (Playwright) (push) Blocked by required conditions
NordaBiz Tests / Smoke Tests (Production) (push) Blocked by required conditions
NordaBiz Tests / Send Failure Notification (push) Blocked by required conditions
user_loader was closing the SQLAlchemy session after loading User, causing lazy-load of company_associations to fail when dashboard template calls can_edit_company(). Eagerly load associations in user_loader. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
650e2a983c
commit
08228d8f12
11
app.py
11
app.py
@ -320,10 +320,17 @@ logger.info("Blueprints registered")
|
|||||||
|
|
||||||
@login_manager.user_loader
|
@login_manager.user_loader
|
||||||
def load_user(user_id):
|
def load_user(user_id):
|
||||||
"""Load user from database"""
|
"""Load user from database with eager-loaded relationships"""
|
||||||
|
from sqlalchemy.orm import joinedload
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
try:
|
try:
|
||||||
return db.query(User).filter_by(id=int(user_id)).first()
|
user = db.query(User).options(
|
||||||
|
joinedload(User.company_associations)
|
||||||
|
).filter_by(id=int(user_id)).first()
|
||||||
|
if user:
|
||||||
|
# Force-load associations before detaching from session
|
||||||
|
_ = user.company_associations
|
||||||
|
return user
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user