Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ filterwarnings =
error:Passing a string to Connection.execute\(\) is deprecated:sqlalchemy.exc.RemovedIn20Warning
# error:"Query" object is being merged into a Session:sqlalchemy.exc.RemovedIn20Warning
# error:"SavedQuery" object is being merged into a Session:sqlalchemy.exc.RemovedIn20Warning
# error:"SqlaTable" object is being merged into a Session:sqlalchemy.exc.RemovedIn20Warning
error:"SqlaTable" object is being merged into a Session:sqlalchemy.exc.RemovedIn20Warning
# error:"SqlMetric" object is being merged into a Session:sqlalchemy.exc.RemovedIn20Warning
# error:"TableColumn" object is being merged into a Session:sqlalchemy.exc.RemovedIn20Warning
# error:"TaggedObject" object is being merged into a Session:sqlalchemy.exc.RemovedIn20Warning
Expand Down
9 changes: 8 additions & 1 deletion superset/connectors/sqla/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1364,7 +1364,14 @@ class SqlaTable(

database: Database = relationship(
"Database",
backref=backref("tables", cascade="all, delete-orphan"),
backref=backref(
"tables",
cascade="all, delete-orphan",
# SQLAlchemy 2.0 behavior: assigning `table.database` no longer
# cascades the SqlaTable into the Database's session; callers must
# add objects to a session explicitly.
cascade_backrefs=False,
),
foreign_keys=[database_id],
)
schema = Column(String(255))
Expand Down
11 changes: 9 additions & 2 deletions superset/examples/generic_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ def safe_serialize(x: Any, column_name: str) -> Optional[str]:

if not tbl:
tbl = SqlaTable(table_name=table_name, database_id=database.id)
# Explicitly add the new table to the session. Assigning `tbl.database`
# below no longer implicitly adds `tbl` to the session (SQLAlchemy 2.0
# behavior, cascade_backrefs=False), so without this, the two
# `db.session.merge()` calls below (one inside `fetch_metadata()`, one
# at the end of this function) would each create a separate transient
# copy of `tbl`, resulting in two pending inserts for the same uuid.
db.session.add(tbl)
# Set the database reference
tbl.database = database

Expand All @@ -187,7 +194,7 @@ def safe_serialize(x: Any, column_name: str) -> Optional[str]:
tbl.fetch_metadata()

db.session.merge(tbl)
db.session.commit()
db.session.commit() # pylint: disable=consider-using-transaction

return tbl

Expand Down Expand Up @@ -242,7 +249,7 @@ def loader(
if description and tbl:
tbl.description = description
db.session.merge(tbl)
db.session.commit()
db.session.commit() # pylint: disable=consider-using-transaction

# Set function name and docstring
loader.__name__ = f"load_{parquet_file}"
Expand Down
2 changes: 2 additions & 0 deletions tests/integration_tests/sqla_models_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ def test_jinja_metrics_and_calc_columns(self, mock_username: MagicMock) -> None:
"'{{ 'xyz_' + time_grain }}' as time_grain",
database=get_example_database(),
)
db.session.add(table)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

TableColumn(
column_name="expr",
expression="case when '{{ current_username() }}' = 'abc' "
Expand Down Expand Up @@ -275,6 +276,7 @@ def test_adhoc_metrics_and_calc_columns(self):
table = SqlaTable(
table_name="test_validate_adhoc_sql", database=get_example_database()
)
db.session.add(table)
db.session.commit()

with pytest.raises(QueryObjectValidationError):
Expand Down
Loading