Skip to content

Conversation

@danf-newton
Copy link

Problem:
When creating media buys, creative objects were being accessed after the database session closed, causing DetachedInstanceError when trying to read attributes like creative.data, creative.format, creative.name.

Root Cause:
SQLAlchemy lazy-loads attributes. When creative objects left the session context, their attributes became inaccessible. Subsequent access in loops triggered DetachedInstanceError.

Solution:
Eagerly load all required attributes into a plain Python dict immediately after querying, while still in session. Store dict instead of ORM object for reads. Keep ORM object reference only for database updates.

Changes:

  • Lines 2794-2807: Create creatives_by_id dict with eager-loaded data
  • Lines 2823+: Use creative_dict values instead of ORM attributes
  • Line 2891: Fix asset 'id' field (Mock adapter compatibility)

Impact:

  • Fixes campaign creation failures during creative assignment
  • No performance impact (same number of queries)
  • Prevents session detachment errors in all code paths

Testing:
Verified with Newton integration test - campaign creation now succeeds without DetachedInstanceError.

Resolves the 'creative not being properly bound to a session' error.

danf-newton and others added 2 commits November 21, 2025 14:35
**Problem:**
When creating media buys, creative objects were being accessed after the
database session closed, causing DetachedInstanceError when trying to read
attributes like creative.data, creative.format, creative.name.

**Root Cause:**
SQLAlchemy lazy-loads attributes. When creative objects left the session
context, their attributes became inaccessible. Subsequent access in loops
triggered DetachedInstanceError.

**Solution:**
Eagerly load all required attributes into a plain Python dict immediately
after querying, while still in session. Store dict instead of ORM object
for reads. Keep ORM object reference only for database updates.

**Changes:**
- Lines 2794-2807: Create creatives_by_id dict with eager-loaded data
- Lines 2823+: Use creative_dict values instead of ORM attributes
- Line 2891: Fix asset 'id' field (Mock adapter compatibility)

**Impact:**
- Fixes campaign creation failures during creative assignment
- No performance impact (same number of queries)
- Prevents session detachment errors in all code paths

**Testing:**
Verified with Newton integration test - campaign creation now succeeds
without DetachedInstanceError.

Resolves the 'creative not being properly bound to a session' error.
Signed-off-by: danf-newton <157386547+danf-newton@users.noreply.github.com>
@bokelley bokelley requested a review from youbek November 25, 2025 20:48
@bokelley
Copy link
Contributor

bokelley commented Dec 7, 2025

@danf-newton sorry this didn't get merged quickly. Do you have a test case we could add here to 1) see if this is still breaking and 2) make sure the fix doesn't regress?

@bokelley bokelley closed this Dec 20, 2025
@github-actions
Copy link
Contributor

IPR Policy Agreement Required

Thank you for your contribution! Before we can accept your pull request, you must agree to our Intellectual Property Rights Policy.

By making a Contribution, you agree that:

  • You grant the Foundation a perpetual, irrevocable, worldwide, non-exclusive, royalty-free copyright license to your Contribution
  • You grant a patent license under any Necessary Claims
  • You represent that you own or have sufficient rights to grant these licenses

To agree, please comment below with the exact phrase:

I have read the IPR Policy

You can read the full IPR Policy here.


I have read the IPR Policy


You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants