SQL Alchemy:表primary_key - 意外的关键字参数错误

时间:2017-04-11 00:11:44

标签: python mysql sqlalchemy keyword

我一直收到有关primary_key=True的以下错误消息。请有人指出我哪里出错了。我想跟着:

http://docs.sqlalchemy.org/en/latest/core/metadata.html#metadata-describing

from sqlalchemy import *

mydata123 = table("dailymarketdata", metadata, 
                    column('data_id', mysql.BIGINT, primary_key=True), 
                    column('dt', Date), 
                    column('security_id', mysql.VARCHAR), 
                    column('open', float), 
                    column('high', float), 
                    column('low', float), 
                    column('close', float), 
                    column('ma200', float), 
                    column('rtrend', mysql.TINYINT))
TypeError                                 Traceback (most recent call last)
<ipython-input-350-bd385371a391> in <module>()
  2 
  3 mydata123 = table("dailymarketdata", metadata, 
----> 4                  column('data_id', mysql.BIGINT, primary_key=True),
  5              column('dt', Date),
  6              column('security_id', mysql.VARCHAR),

TypeError: column() got an unexpected keyword argument 'primary_key'

如果我排除primary_key=True,我会收到其他错误

`AttributeError: 'MetaData' object has no attribute 'key'

1 个答案:

答案 0 :(得分:3)

Python是一种区分大小写的语言

  • columnColumn是sqlalchemy中的不同callables。
  • tableTable也是不同的callables
  • float是python数据类型,而sqlalchemy类型是Float

尝试将您的代码段重写为:

from sqlalchemy import *
from sqlalchemy.dialects import *
metadata = MetaData()
mydata123 = Table("dailymarketdata", metadata, 
                  Column('data_id', mysql.BIGINT, primary_key=True), 
                  Column('dt', Date), 
                  Column('security_id', mysql.VARCHAR), 
                  Column('open', Float), 
                  Column('high', Float), 
                  Column('low', Float), 
                  Column('close', Float), 
                  Column('ma200', Float), 
                  Column('rtrend', mysql.TINYINT))