我有以下代码:
import dbabslayer
from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Table, Column, Integer, String, MetaData
dbObj = dbabslayer.DbAbsLayer();
meta = MetaData()
base = declarative_base()
class TrEnAhmadaliTable(declarative_base()):
__tablename__ = 'tr_en_ahmadali';
id = Column(Integer,primary_key = True,autoincrement = True)
sura = Column(Integer)
aya = Column(Integer)
aya_text = Column(Unicode(convert_unicode=False))
tr_en_aliahmadi = Table('tr_en_ahmadali',base.metadata,
Column(Integer,primary_key = True,autoincrement = False, nullable=False),
Column('sura', Integer, nullable=False),
Column('aya', Integer, nullable=False),
Column('aya_text', UnicodeText(convert_unicode=False), nullable=False)
)
但是出现以下错误:
MetaData(bind=None)
Traceback (most recent call last):
File "./haq.py", line 10, in <module>
from fetchrecordintomemory import *
File "/home/mohsen/codes/haq/fetchrecordintomemory.py", line 5, in <module>
import tables
File "/home/mohsen/codes/haq/tables.py", line 119, in <module>
Column('aya_text', UnicodeText(convert_unicode=False), nullable=False)
File "/usr/lib/python3/dist-packages/sqlalchemy/sql/schema.py", line 469, in __new__
metadata._remove_table(name, schema)
File "/usr/lib/python3/dist-packages/sqlalchemy/util/langhelpers.py", line 67, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/usr/lib/python3/dist-packages/sqlalchemy/util/compat.py", line 277, in reraise
raise value
File "/usr/lib/python3/dist-packages/sqlalchemy/sql/schema.py", line 464, in __new__
table._init(name, metadata, *args, **kw)
File "/usr/lib/python3/dist-packages/sqlalchemy/sql/schema.py", line 557, in _init
self._init_items(*args)
File "/usr/lib/python3/dist-packages/sqlalchemy/sql/schema.py", line 105, in _init_items
item._set_parent_with_dispatch(self)
File "/usr/lib/python3/dist-packages/sqlalchemy/sql/base.py", line 442, in _set_parent_with_dispatch
self._set_parent(parent)
File "/usr/lib/python3/dist-packages/sqlalchemy/sql/schema.py", line 1407, in _set_parent
"Column must be constructed with a non-blank name or "
sqlalchemy.exc.ArgumentError: Column must be constructed with a non-blank name or assign a non-blank .name before adding to a Table.
我该如何解决?
答案 0 :(得分:2)
您的问题似乎与Column(Integer,primary_key = True,autoincrement = False, nullable=False)
有关,请查看the function signature for Column
上的文档。 -列的第一个arg应该是列“名称”,然后是列“类型”。
第一个参数应命名为“ aya_id”。