使用sqlalchemy.types而不是sqlalchemy.dialects.mssql的优点

huangapple go评论68阅读模式
英文:

Advantages of using sqlalchemy.types over sqlalchemy.dialects.mssql

问题

在使用SQLAlchemy保存到SQL Server数据库时,除了可移植性之外,使用sqlalchemy.types比使用sqlalchemy.dialects.mssql设置数据类型是否有优势?

我正在使用以下代码将数据保存到数据库中(利用了pandas的to_sql()函数):

df_members.to_sql(
    'parliament.member_' + extract_date + '',
    schema='source',
    con=engine,
    index=False,
    dtype={
        'id': sqlalchemy.dialects.mssql.SMALLINT(),
        'gender': sqlalchemy.dialects.mssql.NVARCHAR(1),
        'nameDisplayAs': sqlalchemy.dialects.mssql.NVARCHAR(length=256),
        'nameClean': sqlalchemy.dialects.mssql.NVARCHAR(length=256),
        'count': sqlalchemy.dialects.mssql.TINYINT()
    },
)

我注意到以下代码也会产生相同的结果:

df_members.to_sql(
    'parliament.member_' + extract_date + '',
    schema='source',
    con=engine,
    index=False,
    dtype={
        'id': sqlalchemy.types.SMALLINT(),
        'gender': sqlalchemy.types.NVARCHAR(1),
        'nameDisplayAs': sqlalchemy.types.NVARCHAR(length=256),
        'nameClean': sqlalchemy.types.NVARCHAR(length=256),
        'count': sqlalchemy.dialects.mssql.TINYINT()
    },
)

我已经养成了使用前者的习惯,因为这样我就不必记住哪些数据类型是特定于供应商的(例如,TINYINT()),哪些不是。

但是,假设可移植性不是一个问题(如果将来更改供应商,代码仍然需要更新以删除TINYINT()引用),这种方法是否存在缺点,比如速度?

英文:

When saving to a SQL Server database using SQLAlchemy are there advantages to setting datatypes using sqlalchemy.types over sqlalchemy.dialects.mssql besides portability?

I'm saving data to a database using the following (making use of pandas' to_sql()):

df_members.to_sql(
    'parliament.member_' + extract_date + '',
    schema='source',
    con=engine,
    index=False,
    dtype={
        'id': sqlalchemy.dialects.mssql.SMALLINT(),
        'gender': sqlalchemy.dialects.mssql.NVARCHAR(1),
        'nameDisplayAs': sqlalchemy.dialects.mssql.NVARCHAR(length=256),
        'nameClean': sqlalchemy.dialects.mssql.NVARCHAR(length=256),
        'count': sqlalchemy.dialects.mssql.TINYINT()
    },
)

I note that the following yields the same results:

df_members.to_sql(
    'parliament.member_' + extract_date + '',
    schema='source',
    con=engine,
    index=False,
    dtype={
        'id': sqlalchemy.types.SMALLINT(),
        'gender': sqlalchemy.types.NVARCHAR(1),
        'nameDisplayAs': sqlalchemy.types.NVARCHAR(length=256),
        'nameClean': sqlalchemy.types.NVARCHAR(length=256),
        'count': sqlalchemy.dialects.mssql.TINYINT()
    },
)

I've got in the habit of using the former because it saves me having to remember which datatypes are vendor-specific (e.g. TINYINT()) and which aren't.

But, assuming portability isn't a concern (if we ever changed vendor the code would need updating anyway to remove the TINYINT() references) are there disadvantages to this approach - for example, speed?

答案1

得分: 1

以下是代码部分的翻译:

sqlalchemy/lib/sqlalchemy/dialects/mssql/init.py 中:

from .base import SMALLINT

sqlalchemy/lib/sqlalchemy/dialects/mssql/base.py 中:

from ...types import SMALLINT

sqlalchemy/lib/sqlalchemy/types.py 中:

from .sql.sqltypes import SMALLINT as SMALLINT

继续查看:

sqlalchemy/lib/sqlalchemy/sql/sqltypes.py 中:

class SMALLINT(SmallInteger):
    """SQL SMALLINT类型。
    .. 参见::

        :class:`_types.SmallInteger` - 基本类型的文档。
    """
    __visit_name__ = "SMALLINT"

因此,对于许多(甚至所有?)大写类型,使用 sqlalchemy.types.XXXsqlalchemy.dialects.YYY.XXX 看起来是相同的。所有大写数据类型都继承自驼峰大小写的数据类型(参考这里)。

英文:

I don't know if this answer to your question but using the source code:

In sqlalchemy/lib/sqlalchemy/dialects/mssql/init.py:

from .base import SMALLINT

In sqlalchemy/lib/sqlalchemy/dialects/mssql/base.py

from ...types import SMALLINT

In sqlalchemy/lib/sqlalchemy/types.py <<< sqlalchemy.types.SMALLINT

from .sql.sqltypes import SMALLINT as SMALLINT

Go further

In sqlalchemy/lib/sqlalchemy/sql/sqltypes.py

class SMALLINT(SmallInteger):
    &quot;&quot;&quot;The SQL SMALLINT type.
    .. seealso::

        :class:`_types.SmallInteger` - documentation for the base type.
    &quot;&quot;&quot;
    __visit_name__ = &quot;SMALLINT&quot;

So for many (all?) UPPERCASE types, using sqlalchemy.types.XXX or sqlalchemy.dialects.YYY.XXX seems to be the same thing. All UPPERCASE datatypes are inherited from CamelCase datatypes (ref)

huangapple
  • 本文由 发表于 2023年3月31日 17:31:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896906.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定