Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: old unique constraint remnance #9882

Closed
Closed
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
6 changes: 5 additions & 1 deletion superset/connectors/sqla/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,11 @@ class SqlaTable(Model, BaseDatasource):
owner_class = security_manager.user_model

__tablename__ = "tables"
__table_args__ = (UniqueConstraint("database_id", "table_name"),)
__table_args__ = (
UniqueConstraint(
"database_id", "schema", "table_name", name="_customer_location_uc",
),
)
Comment on lines +376 to +380
Copy link
Member

Choose a reason for hiding this comment

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

Ping @john-bodley . I've been meaning to bring this up for discussion, as I also feel schema should be included in the constraint. However, I remember you had done work in this area before, and there was some reason why it wasn't possible to include schema in the unique constraint.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is a viable solution based on how different engine treat uniqueness for nullable columns (schema in this case). This is documented in #5449 which is an open PR from over two years ago but was updated last year to remedy the nullable issue. Though it's not perfect, i.e., ideally the constraint should reside within the database model I sense it's the only viable solution unless we special case the various engines.

Copy link
Member Author

Choose a reason for hiding this comment

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

hey @john-bodley @villebro , sorry to bring this back, but how about we just get rid of the constraint altogether? You want 2 datasets pointing to the same table, why not?

At least we should alter the old migration that creates the old/bad constraint we then cannot delete successfuly

Copy link
Member

Choose a reason for hiding this comment

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

Having multiple Superset datasources with the same schema and table name for a given database does not sense to me as it is not easily apparent from the current UI which datasource is being referenced as the name if the primary indicator throughout the application.


table_name = Column(String(250), nullable=False)
main_dttm_col = Column(String(250))
Expand Down
4 changes: 3 additions & 1 deletion superset/migrations/versions/4e6a06bad7a8_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ def upgrade():
"changed_by_fk", sa.Integer(), sa.ForeignKey("ab_user.id"), nullable=True
),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("table_name"),
# Commenting out spring 2020 to avoid flimsy delete in
# subsequent migration b4456560d4f3_change_table_unique_constraint.py
# sa.UniqueConstraint("table_name"),
)
op.create_table(
"columns",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,24 @@
revision = "b4456560d4f3"
down_revision = "bb51420eaf83"

conv = {
"uq": "uq_%(table_name)s_%(column_0_name)s",
}


def upgrade():
try:
# Trying since sqlite doesn't like constraints
op.drop_constraint("tables_table_name_key", "tables", type_="unique")
op.create_unique_constraint(
"_customer_location_uc", "tables", ["database_id", "schema", "table_name"]
)
with op.get_context().autocommit_block():
with op.batch_alter_table("tables", naming_convention=conv) as batch_op:
# Trying to drop the constraint if it exists
batch_op.drop_constraint("tables_table_name_key", type_="unique")
except Exception:
pass
with op.batch_alter_table("tables", naming_convention=conv) as batch_op:
batch_op.create_unique_constraint(
"_customer_location_uc", ["database_id", "schema", "table_name"],
)


def downgrade():
try:
# Trying since sqlite doesn't like constraints
op.drop_constraint(u"_customer_location_uc", "tables", type_="unique")
except Exception:
pass
op.drop_constraint("_customer_location_uc", "tables", type_="unique")