简体   繁体   English

SQLAlchemy TypeError:__init __()为参数'name'获得了多个值

[英]SQLAlchemy TypeError: __init__() got multiple values for argument 'name'

I'm getting a strange error in SQL Alchemy when I try to define both a uniqueness constraint and an index for a given table. 当我尝试为给定表定义唯一性约束和索引时,我在SQL Alchemy中遇到一个奇怪的错误。

from sqlalchemy.sql import func
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, BigInteger, \
    String, Numeric, DateTime, Boolean
from sqlalchemy.dialects.postgresql import ARRAY
from sqlalchemy.schema import Index, UniqueConstraint
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
import sqlalchemy


Base = declarative_base()

class CommonColumns(object):

    id = Column(Integer,
        primary_key=True,
        autoincrement=True
    )
    created_at = Column(
        DateTime,
        server_default=func.now()
    )
    updated_at = Column(
        DateTime,
        server_default=func.now(),
        server_onupdate=func.now()
    )

class Example(CommonColumns, Base):

    __tablename__ = 'encoding'

    model_name = Column(String)
    model_version = Column(String)
    product_id = Column(BigInteger)
    position = Column(Integer)
    file_name = Column(String)
    image_type = Column(String)
    encoding = Column(ARRAY(Numeric))
    sku = Column(String)

    __table_args__ = (
        UniqueConstraint(
            model_name,
            model_version,
            product_id,
            position,
            file_name,
            image_type,
            name='example_unique'
        ),
        Index(
            model_name,
            model_version,
            product_id,
            position,
            file_name,
            image_type,
            name='example_index'
        ),
        {}
    )


connection_string = sqlalchemy.engine.url.URL(
    drivername='<removed>',
    username='<removed>',
    password='<removed>',
    database='<removed>',
    host='<removed>'
)
engine = create_engine(connection_string)
Session = sessionmaker(bind=engine)
session = Session()

Base.metadata.create_all(engine)
session.commit()

This code results in the following error: 此代码导致以下错误:

Traceback (most recent call last): File "/Users/ryan.zotti/Documents/repos/recommendations/name_example.py", line 30, in class Example(CommonColumns, Base): File "/Users/ryan.zotti/Documents/repos/recommendations/name_example.py", line 60, in Example name='encoding_index' TypeError: init () got multiple values for argument 'name' 追溯(最近一次通话最近):类“示例(CommonColumns,基本)”中的文件“ /Users/ryan.zotti/Documents/repos/recommendations/name_example.py”,第30行:文件“ /Users/ryan.zotti/Documents /repos/recommendations/name_example.py“,第60行,在示例中name ='encoding_index'TypeError: init ()获得了参数'name'的多个值

Why does the name collision occur? 为什么会发生名称冲突? I define name separately for UniqueConstraint and Index . 我分别为UniqueConstraintIndex定义name

I'm using SQLAlchemy==1.3.8 and Postgres 9.5. 我正在使用SQLAlchemy==1.3.8和Postgres 9.5。

The collision was between model_name and name="example_index" . 冲突发生在model_namename="example_index" The first arg of the Index __init__ method is the name, so try this: Index __init__方法的第一个参数是名称,因此请尝试以下操作:

Index(
    "example_index",
    model_name,
    model_version,
    product_id,
    position,
    file_name,
    image_type
)

From: https://docs.sqlalchemy.org/en/13/core/constraints.html#sqlalchemy.schema.Index 来自: https : //docs.sqlalchemy.org/zh/13/core/constraints.html#sqlalchemy.schema.Index

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:“ __ init __()为关键字参数&#39;name&#39;获得了多个值” - TypeError: “__init__() got multiple values for keyword argument 'name'” TypeError:__init __()为参数&#39;n_splits&#39;获取了多个值 - TypeError: __init__() got multiple values for argument 'n_splits' TypeError:__init __()为参数&#39;fieldnames&#39;获得了多个值 - TypeError: __init__() got multiple values for argument 'fieldnames' 类型错误:__init__() 为参数“strides”获得了多个值 - TypeError: __init__() got multiple values for argument 'strides' TypeError:__init__() 为参数“轴”获取了多个值 - TypeError: __init__() got multiple values for argument 'axes' Python TypeError:__ init __()为参数&#39;master&#39;获取了多个值 - Python TypeError: __init__() got multiple values for argument 'master' TypeError:__init __()为关键字参数“ choices”获得了多个值 - TypeError: __init__() got multiple values for keyword argument 'choices' TypeError: __init__() 为参数 'center' 获得了多个值 - TypeError: __init__() got multiple values for argument 'center' TypeError:__ init __()得到关键字参数&#39;customer&#39;的多个值 - TypeError: __init__() got multiple values for keyword argument 'customer' 类型错误:__init__() 为参数“index”获得了多个值 - TypeError: __init__() got multiple values for argument 'index'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM