使用下面的类和底部的测试程序,我得到格式错误的Insert,其中Application_Id的值写为'(n,)',其中' n' s已插入的值,即如果Application_Id应为2,则值'(2,')就在其中。
我无法找到原因,因为据我所知,其他外键列的处理方式没有区别。 (排除所有进口 - 这不是问题)。
我在Windows 10 + Visual Studio 2015上使用Python 3.4.4和SQLAlchemy 1.1.5。切换到另一个操作系统或IDE不是一个选项: - (
时间跨度
class Timespan(Base):
"""
"""
__tablename__ = 'timespan'
def __init__(self, start, end, account, application, language):
self.StartTime = start
self.EndTime = end
self.Account_Id = account
self.Application_Id = application,
self.Language_Id = language
StartTime = Column(DateTime)
EndTime = Column(DateTime)
Id = Column(Integer, primary_key = True)
Account_Id = Column(Integer, ForeignKey('account.Id'))
Application_Id = Column(Integer, ForeignKey('application.Id'))
Language_Id = Column(Integer, ForeignKey('language.Id'))
应用
class Application(Base):
"""
"""
__tablename__ = 'application'
Id = Column(Integer, primary_key = True)
Code = Column(String)
Description = Column(String)
def __init__(self, code, description):
self.Code = code
self.Description = description
帐户
class Account(Base):
"""
"""
__tablename__ = 'account'
def __init__(self, uid, domain):
self.MvidDomain = domain
self.MvidUid = uid
Id = Column(Integer, primary_key = True)
MvidUid = Column(Integer)
MvidDomain = Column(String)
语言
class Language(Base):
"""
"""
def __init__(self, description, code):
self.Description = description
self.Code = code
__tablename__ = 'language'
Id = Column(Integer, primary_key = True)
Description = Column(String)
Code = Column(String)
测试程序
class TimespanTest(TestCase):
def test_insert(self):
engine = create_engine('sqlite://', echo = True)
Base.metadata.create_all(engine)
Session = sessionmaker(bind = engine)
session = Session()
application = Application(code = 'itw', description = 'desc')
lang = Language(description = 'test language', code = 'da')
account = Account(uid = 'test_user', domain = 'test_domain')
session.add(lang)
session.add(application)
session.add(account)
session.commit()
print (lang.Id)
print (application.Id)
print (account.Id)
timespan = Timespan(
datetime.today(), datetime.today(),
account = account.Id,
application = application.Id,
language = lang.Id)
session.add(timespan)
session.commit()
调试输出/例外:
(sqlite3.InterfaceError) Error binding parameter 3 - probably unsupported type.
[SQL: 'INSERT INTO timespan ("StartTime", "EndTime", "Account_Id", "Application_Id", "Language_Id") VALUES (?, ?, ?, ?, ?)']
[parameters: ('2017-01-24 08:29:23.447994', '2017-01-24 08:29:23.447994', 1, (1,), 1)]
问题出现在倒数第二个参数中,即值(1,),它应该只是1。
答案 0 :(得分:3)
Timespan 类中存在问题,请注意 = 后,,删除此项并继续工作。以下代码删除了逗号: -
class Timespan(Base):
"""
"""
__tablename__ = 'timespan'
def __init__(self, start, end, account, application, language):
self.StartTime = start
self.EndTime = end
self.Account_Id = account
self.Application_Id = application
self.Language_Id = language
StartTime = Column(DateTime)
EndTime = Column(DateTime)
Id = Column(Integer, primary_key = True)
Account_Id = Column(Integer, ForeignKey('account.Id'))
Application_Id = Column(Integer, ForeignKey('application.Id'))
Language_Id = Column(Integer, ForeignKey('language.Id'))