@@ -276,10 +276,14 @@ def init(cursor: duckdb.DuckDBPyConnection) -> None:
276
276
# set it as the default catalog.
277
277
# If a user tried to attach a MotherDuck database/share which has already by attached via
278
278
# `ATTACH 'md:'`, then we don't want to raise since this is expected.
279
- if not (
280
- 'database with name "memory" already exists' in str (e )
281
- and path_options == ":memory:"
282
- ) and f'database with name "{ alias } " already exists' not in str (e ):
279
+ if (
280
+ not (
281
+ 'database with name "memory" already exists' in str (e )
282
+ and path_options == ":memory:"
283
+ )
284
+ and f"""database with name "{ path_options .path .replace ('md:' , '' )} " already exists"""
285
+ not in str (e )
286
+ ):
283
287
raise e
284
288
if i == 0 and not getattr (self , "database" , None ):
285
289
cursor .execute (f"USE { alias } " )
@@ -373,12 +377,17 @@ def to_sql(self, alias: str) -> str:
373
377
options .append (f"TYPE { self .type .upper ()} " )
374
378
if self .read_only :
375
379
options .append ("READ_ONLY" )
380
+ options_sql = f" ({ ', ' .join (options )} )" if options else ""
381
+ alias_sql = ""
376
382
# TODO: Add support for Postgres schema. Currently adding it blocks access to the information_schema
377
- alias_sql = (
383
+ if self . type == "motherduck" :
378
384
# MotherDuck does not support aliasing
379
- f" AS { alias } " if not (self .type == "motherduck" or self .path .startswith ("md:" )) else ""
380
- )
381
- options_sql = f" ({ ', ' .join (options )} )" if options else ""
385
+ if (md_db := self .path .replace ("md:" , "" )) != alias .replace ('"' , "" ):
386
+ raise ConfigError (
387
+ f"MotherDuck does not support assigning an alias different from the database name { md_db } ."
388
+ )
389
+ else :
390
+ alias_sql += f" AS { alias } "
382
391
return f"ATTACH '{ self .path } '{ alias_sql } { options_sql } "
383
392
384
393
0 commit comments