Closed
Description
Describe the bug
The MSDialect.has_table method handles temp tables with a query like
select from tempdb.INFORMATION_SCHEMA.columns where table_name like '{table_name}___%'
If there is another active session using a temp table of the same name, this version of has_table() returns True, even though there is no such table available to this session.
To Reproduce
con1 = engine.connect()
con1.execute('create table #myveryveryuniquetemptablename (a int)')
con2 = engine.connect()
print(con2.dialect.has_table(con2, '#myveryveryuniquetemptablename'))
con1.close()
con2.close()
Error
Above snippet prints True. Correct behavior would be False.
Versions
- OS: Windows
- Python: 3.9
- SQLAlchemy: 1.4.23
- Database: MS SQL Server
- DBAPI: pyodbc 4.0.31
Additional context
No response
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
ewallace-RI commentedon Aug 19, 2021
Introduced by the fix for #5597.
CaselIT commentedon Aug 19, 2021
Thanks for reporting.
Do you have a suggestion regarding what's the correct query to do instead?
CaselIT commentedon Aug 19, 2021
also cc @gordthompson
ewallace-RI commentedon Aug 19, 2021
Sorry, should have included the suggested fix. I use something like this, which works for both temp tables and physical tables.
Not sure if there's some other preferred method.
sqla-tester commentedon Aug 20, 2021
Gord Thompson has proposed a fix for this issue in the master branch:
Fix has_table() to exclude other sessions' temp tables https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/3014
zzzeek commentedon Aug 20, 2021
I noted on the gerrit that the above fix should not apply to global temporary tables that start with "##".
ewallace-RI commentedon Aug 20, 2021
@zzzeek I think the attached fix works for global temp tables. Temp tables from the current session and ##global temp tables will both have non-null object_ids, so this version of has_table will return True for both of them, as expected. And the _temp_table_name_like_pattern logic is unaffected.
@gordthompson I also noticed when looking into this that the _temp_table_name_like_pattern doesn't seem to be quite right. The unquoted underscore is a single-character wildcard in a like pattern, so '#tmp___%' will also match a table named '#tmp123'. The object_id logic should do the right thing, so I think the proposed fix also takes care of that, just something to be aware of.
zzzeek commentedon Aug 20, 2021
oh, good idea.
gordthompson commentedon Aug 20, 2021
Good point. Thanks for mentioning it!
8 remaining items