我有一个现有的MariaDB数据库,我正在迁移该数据库以使用Flask sqlalchemy。我没有重建所有模型,而是使用db.Model.metadata.tables
为自己创建了模型
我现在正在尝试创建记录,其中主键fileid
是数据库通过uuid_short()
自动生成的值。我得到了FlushError: Instance has a NULL identity key
对于如何告诉sqlalchemy数据库将填充此列,我有点不知所措。我试图手动将fileid定义添加到类中,但这只会产生错误Can't add additional column 'fileid' when specifying __table__
我当前的文件类如下所示
class Files(db.Model):
__table__ = db.Model.metadata.tables['files']
def __repr__(self):
return self.fileid
def __str__(self):
return f"[File {self.fileid}] {self.localpath} by {self.uploadUser}"
要创建新记录,我将填充除fileid
之外的所有列,如下所示
f = Files(
localpath=filename_final,
uploadUser=user.userid,
uploadTime=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
originalFilename=received_file.filename,
mimeType=received_file.mimetype,
status='ok'
)
db.session.add(f)
db.session.commit()
非常感谢您的帮助
目前没有回答
相关问题 更多 >
编程相关推荐