Possible error with fully qualified table names

54 views
Skip to first unread message

Benjamin Taub

unread,
Dec 22, 2020, 12:35:51 PM12/22/20
to sqlalchemy
schema.py defines a variable fullname for tables as follows:

if self.schema is not None:
    self.fullname = "%s.%s" % (self.schema, self.name)
else:
    self.fullname = self.name

However, this fails for me in some cases, apparently when my table or schema name starts with a number. To address this, shouldn't self.fullname be surrounded by delimiters (e.g. "`%s`.`%s`")?

FWIW, I'm working in MySQL.

Thanks!
Ben

Mike Bayer

unread,
Dec 22, 2020, 1:17:59 PM12/22/20
to noreply-spamdigest via sqlalchemy
the .fullname attribute is not used directly in SQL statements, when a schema or table name requires quoting the compiler will apply them as needed.

from sqlalchemy import Column
from sqlalchemy import String
from sqlalchemy.dialects import mysql
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session

Base = declarative_base()


class SomeTable(Base):
    __tablename__ = "3table"

    id = Column(String, primary_key=True)

    __table_args__ = {"schema": "4schema"}


s = Session()

print(s.query(SomeTable).statement.compile(dialect=mysql.dialect()))

output:

SELECT `4schema`.`3table`.id
FROM `4schema`.`3table`
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

Benjamin Taub

unread,
Dec 22, 2020, 1:25:42 PM12/22/20
to sqlalchemy
Thank you, Mike. In my case, I am writing a TRUNCATE TABLE statement. I don't think SQLAlchemy provides that so I'm writing it in text and having the engine execute it rather than having SQLAlchemy generate it. Are you recommending, therefore, that I not use the fullname attribute for this?

Mike Bayer

unread,
Dec 22, 2020, 1:31:42 PM12/22/20
to noreply-spamdigest via sqlalchemy
you could use "fullname" but you would need to apply string splitting across the dot and then quoting of the individual tokens.

assuming you are using case insensitive identifiers it would be safe to generically do the above for all strings if you wanted:

qualified_name = ".".join(
   "`%s`" % token for token in fullname.split(".")
)

SQLAlchemy has quoting facilities also but they are specific to using the compiler, so if you aren't using the @compiles extension then you would do whatever works to get the quoting you need in your SQL statement.

Benjamin Taub

unread,
Dec 22, 2020, 1:57:01 PM12/22/20
to sqlal...@googlegroups.com
Perfect! Thank you, Mike.
Ben
__________________________________________
Dataspace
Benjamin Taub, CEO
Check out Golden Record, our cloud-based record matching and deduplication solution
NOTE: I usually check email only 2 - 3 times / day.  For issues needing immediate attention, please call.



You received this message because you are subscribed to a topic in the Google Groups "sqlalchemy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sqlalchemy/PCUvHcDatvM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sqlalchemy+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/6ee23436-8fe5-40d6-b2c0-d1d9545b729a%40www.fastmail.com.
Reply all
Reply to author
Forward
0 new messages