[英]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
. 我分别为
UniqueConstraint
和Index
定义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_name
和name="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.