codeant-ai-for-open-source[bot] commented on code in PR #42222:
URL: https://github.com/apache/superset/pull/42222#discussion_r3616105886
##########
tests/integration_tests/sqla_models_tests.py:
##########
@@ -998,18 +1009,20 @@ def test_extra_cache_keys_in_dataset_metrics_and_columns(
mock_username: Mock,
mock_user_id: Mock,
):
+ columns = [
+ TableColumn(column_name="user", type="VARCHAR(255)"),
+ TableColumn(
+ column_name="username",
+ type="VARCHAR(255)",
+ expression="{{ current_username() }}",
+ ),
+ ]
+ db.session.add_all(columns)
table = SqlaTable(
table_name="test_has_no_extra_cache_keys_table",
sql="SELECT 'abc' as user",
database=get_example_database(),
- columns=[
- TableColumn(column_name="user", type="VARCHAR(255)"),
- TableColumn(
- column_name="username",
- type="VARCHAR(255)",
- expression="{{ current_username() }}",
- ),
- ],
+ columns=columns,
Review Comment:
**Suggestion:** `TableColumn` objects are added to the session before they
are attached to a table, and then `get_example_database()` is called while
building the dataset. That call is likely to execute a query, which triggers
SQLAlchemy autoflush and can try to insert these columns with a null
`table_id`, causing an integrity error. Attach the columns to the table before
adding them to the session (or defer `add_all` until after table construction /
use `no_autoflush`). [logic error]
<details>
<summary><b>Severity Level:</b> Major ⚠️</summary>
```mdx
- ❌ `test_extra_cache_keys_in_dataset_metrics_and_columns` may raise
IntegrityError.
- ❌ Integration test suite / CI run can be blocked.
- ⚠️ Flaky behavior if database constraints differ between environments.
```
</details>
<details>
<summary><b>Steps of Reproduction ✅ </b></summary>
```mdx
1. Run the integration test
`test_extra_cache_keys_in_dataset_metrics_and_columns` in
`tests/integration_tests/sqla_models_tests.py:1008`.
2. Inside this test, at lines 1012–1020, two `TableColumn` instances are
created **without
a `table`/`table_id` assigned** and immediately added to the session via
`db.session.add_all(columns)` at line 1020.
3. Still in the same test, at line 1021, `SqlaTable` is instantiated with
`database=get_example_database()` (imported from `superset.utils.database`),
which
internally performs a `db.session.query(...)` to fetch the example
`Database` record,
causing SQLAlchemy to autoflush the pending `TableColumn` objects.
4. During this autoflush, SQLAlchemy attempts to INSERT the two
`TableColumn` rows while
their `table_id` is still `NULL` (they are not yet linked to `table` because
`SqlaTable(..., columns=columns)` hasn’t run), which can raise an
`IntegrityError` if
`TableColumn.table_id` is non-nullable, causing this test (and therefore the
integration
test suite/CI run) to fail.
```
</details>
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=5f0bbb80ffdf458bb3e39dae78135a17&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
[](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=5f0bbb80ffdf458bb3e39dae78135a17&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
*(Use Cmd/Ctrl + Click for best experience)*
<details>
<summary><b>Prompt for AI Agent 🤖 </b></summary>
```mdx
This is a comment left during a code review.
**Path:** tests/integration_tests/sqla_models_tests.py
**Line:** 1012:1025
**Comment:**
*Logic Error: `TableColumn` objects are added to the session before
they are attached to a table, and then `get_example_database()` is called while
building the dataset. That call is likely to execute a query, which triggers
SQLAlchemy autoflush and can try to insert these columns with a null
`table_id`, causing an integrity error. Attach the columns to the table before
adding them to the session (or defer `add_all` until after table construction /
use `no_autoflush`).
Validate the correctness of the flagged issue. If correct, How can I resolve
this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask
user if the user wants to fix the rest of the comments as well. if said yes,
then fetch all the comments validate the correctness and implement a minimal fix
```
</details>
<a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42222&comment_hash=24daddde8818323c2a42d03295285025b16b1817a4f1473e11e9b5a6a5eaf755&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42222&comment_hash=24daddde8818323c2a42d03295285025b16b1817a4f1473e11e9b5a6a5eaf755&reaction=dislike'>👎</a>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]