Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Alchemy : Table primary_key - unexpected keyword argument error

I keep getting the below error regarding the primary_key=True . Please could someone point out where i'm going wrong. I'm trying to follow:

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'

If I exclude primary_key=True I get another error

`AttributeError: 'MetaData' object has no attribute 'key'
like image 268
Matthewj28 Avatar asked Apr 11 '17 00:04

Matthewj28


1 Answers

Python is a case sensitive language

  • column and Column are different callables in sqlalchemy.
  • table and Table are also different callables
  • float is the python data type, while the sqlalchemy type is Float

Try, rewriting your snippet as:

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))
like image 85
Haleemur Ali Avatar answered Oct 17 '22 21:10

Haleemur Ali