superset icon indicating copy to clipboard operation
superset copied to clipboard

Cannot create table with same name from a different datasource

Open frankfarrell opened this issue 9 years ago • 27 comments

Make sure these boxes are checked before submitting your issue - thank you!

  • [ ] I have checked the superset logs for python stacktraces and included it here as text if any
  • [ ] I have reproduced the issue with at least the latest released version of superset
  • [ ] I have checked the issue tracker for the same issue and I haven't found one similar

Superset version

0.19.1

Expected results

That you could add the same table from a differnet datasource

Actual results

(raised as a result of Query-invoked autoflush; consider using a session.no_autoflush block if this flush is occurring prematurely) (sqlite3.IntegrityError) UNIQUE constraint failed: tables.table_name [SQL: 'INSERT INTO tables (created_on, changed_on, description, default_endpoint, is_featured, filter_select_enabled, "offset", cache_timeout, params, perm, table_name, main_dttm_col, database_id, fetch_values_predicate, user_id, schema, sql, created_by_fk, changed_by_fk) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'] [parameters: ('2017-11-10 16:36:45.635385', '2017-11-10 16:36:45.635417', None, None, 0, 0, 0, None, None, None, '<table_name>', None, 2, None, None, '', None, None, None)]

Steps to reproduce

We have a few datasources using the same schema. We would like to configure query tables from each.

It seems an easy fix would be just to change this line: https://github.com/apache/incubator- superset/blob/master/superset/migrations/versions/4e6a06bad7a8_init.py#L84 to sa.UniqueConstraint('table_name', 'database_id'). This line does have such a constraint ( with schema too), https://github.com/apache/incubator-superset/blob/1ea4521d0c116d96b51749613347279669529dd0/superset/connectors/sqla/models.py#L188

Its similar to https://github.com/apache/incubator-superset/pull/3583/files, so perhaps a fix could be included in that PR too?

Thanks, great project by the way!

frankfarrell avatar Nov 10 '17 16:11 frankfarrell

Feel free to open a PR

xrmx avatar Nov 10 '17 16:11 xrmx

Will do

frankfarrell avatar Nov 10 '17 17:11 frankfarrell

I think this open PR solves this: https://github.com/apache/incubator-superset/pull/3583

mistercrunch avatar Nov 13 '17 16:11 mistercrunch

superset-table-bug This is the error. Its very similar to #3583 but not the same. For slices it may be necessary to show <table_name>/ rather than just table name?

frankfarrell avatar Nov 15 '17 15:11 frankfarrell

https://github.com/apache/incubator-superset/compare/master...frankfarrell:tables-constraints?expand=1 It just needs the alembic upgrade/downgrade functiosn to match now

frankfarrell avatar Nov 15 '17 17:11 frankfarrell

Root cause here is that the alembic migrations scripts first creates a unique constraint on table name: https://github.com/apache/incubator-superset/blob/master/superset/migrations/versions/4e6a06bad7a8_init.py#L84 Later revisions try to create the correct unique constraint on table)name, schema and database: https://github.com/apache/incubator-superset/blob/master/superset/migrations/versions/b4456560d4f3_change_table_unique_constraint.py#L21 and https://github.com/apache/incubator-superset/blob/master/superset/migrations/versions/3b626e2a6783_sync_db_with_models.py#L65

However, sqlite doesn't allow dropping unnamed unique constraints: https://stackoverflow.com/questions/42013265/remove-unique-constraint-on-a-column-in-sqlite-database

Solution would be to rename table, create new table and copy over data in a new migration. Or use some other db :)

frankfarrell avatar Nov 16 '17 14:11 frankfarrell

We could also alter the creation of the constraint in the first place. Won't help existing installations but it will help future ones.

mistercrunch avatar Nov 20 '17 04:11 mistercrunch

@mistercrunch That's also an option, I wasn't sure if you wanted to keep the migration scripts immutable or not. Chances are that nobody will upgrade an sqlite db in place. In any case I have the correct migration done in the PR: #3885, which fixes the issue

frankfarrell avatar Nov 20 '17 12:11 frankfarrell

In general we want to keep the migrations static but in cases like this I think it's ok to alter them, as long as downstream migration account for both branches in history.

I'll let @john-bodley review the PR since he's done something very similar on the Druid side recently.

mistercrunch avatar Nov 20 '17 16:11 mistercrunch

@frankfarrell are you certain you weren't able to delete an unnamed uniqueness constraint for SQLite? There's a section on the Alembic documentation about dealing with constraints in SQLite.

I was able to leverage this logic for dropping uniqueness constraints here

john-bodley avatar Nov 20 '17 17:11 john-bodley

@john-bodley I am not certain no. I can try that tomorrow, thanks for the info!

These 2 following constraint modifications didn't work : https://github.com/apache/incubator-superset/blob/15b67b2c6c3c2982f6620fce5d30bd05951458f7/superset/migrations/versions/b4456560d4f3_change_table_unique_constraint.py#L19 https://github.com/apache/incubator-superset/blob/15b67b2c6c3c2982f6620fce5d30bd05951458f7/superset/migrations/versions/3b626e2a6783_sync_db_with_models.py#L63

frankfarrell avatar Nov 20 '17 17:11 frankfarrell

I'm having the same issue. Is this still being worked on?

jerowe avatar Jan 05 '18 15:01 jerowe

I created this PR https://github.com/apache/incubator-superset/pull/3885 which solves the issue. Its uses plain SQL, I haven't updated it to use alembic as described by @john-bodley above. Should I do that or does it look ok to merge as is?

frankfarrell avatar Jan 05 '18 16:01 frankfarrell

Any news by chance ?

asyd avatar Apr 04 '18 08:04 asyd

Oh, just tested 0.24.0 and seem it's working, so maybe someone should close this issue?

asyd avatar Apr 04 '18 09:04 asyd

I am using 0.25.6 . This still seems to be an issue. screen shot 2018-07-02 at 2 35 46 pm I tried adding another nyc_data_external from a different database customer_data, which also happens to have nyc_data_external table. The error was still thrown, @mistercrunch

SanjayJosh avatar Jul 02 '18 09:07 SanjayJosh

I was thinking, since users generally don't migrate once the app is initialised, how about we add the script to re configure the tables table for sqlite3 as a part of the code when superset init runs. It could reside well as utils function.

SanjayJosh avatar Jul 02 '18 12:07 SanjayJosh

@SanjayJosh it might be worth opening a new issue, this may be a regression.

Preston4tw avatar Jul 06 '18 20:07 Preston4tw

I am using 0.25.6 . This still seems to be an issue. screen shot 2018-07-02 at 2 35 46 pm I tried adding another nyc_data_external from a different database customer_data, which also happens to have nyc_data_external table. The error was still thrown, @mistercrunch

@SanjayJosh It seems there's no new issue about the regression? Or did you just found it's gone?

raof01 avatar May 19 '19 14:05 raof01

I just can'( believe it's not possible to have two databases with tables having the same name??!! I must be missing something ... am I ?

FredericLatour avatar Feb 24 '20 22:02 FredericLatour