为什么我的数据库在向另一个数据库添加数据时变空



当我尝试将数据添加到images.db数据库时,我有两个数据库连接到我的烧瓶服务器,users.db中的数据会自动删除。早些时候,这两个表都在同一个数据库中,但现在即使创建了两个独立的数据库,问题也没有得到解决。

这是型号.py 的代码

class Image(db.Model): #images.db
id = db.Column(db.Integer, primary_key=True)
date_observed = db.Column(db.DateTime, unique=True, nullable=False)
jd = db.Column(db.Float, unique=True, nullable=False)
def __repr__(self):
attrs = vars(self)
return ', '.join("%s: %s" % item for item in attrs.items())

# User table in the database
class User(db.Model, UserMixin):
__bind_key__ = 'users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
password = db.Column(db.String(60), nullable=False)
history = db.Column(db.String(240))
def __repr__(self):
return "User({self.username}, {self.email})".format()
def file_to_Image_obj(fits_image_filename):

hdul = fits.open(fits_image_filename)
hdr = hdul[0].header
(y_end,x_end) = hdul[0].data.shape 
hdul.close()
wcs = WCS(hdr)

n_of_div = 5
this_image = Image(
date_observed = parse(read_header('DATE-OBS',hdr)),
jd = read_header('JD',hdr)
)

return this_image
# takes as input directory path and adds all fits files (.fits extension) to the db
def add_dir_to_db(dirpath, append=True):
print('Adding Images to Database')
if not append:
db.drop_all()
db.create_all()

for dirpath, dirnames, filenames in os.walk(dirpath):
for filename in filenames:
filepath = os.path.join(dirpath, filename)
#changed to proc.fits for demo
if filepath.endswith('.fits'):
try :
this_image = file_to_Image_obj(filepath)
print(filepath, this_image.date_observed)
db.session.add(this_image)
except Exception as e :
with open("error_report.txt","a") as logf:
logf.write("Failed to make db object {0}: {1}n".format(filepath, str(e)))
print (str(e))


db.session.commit()
def add_user_to_db(username, email, password):
print('Adding User')
user = User(username=username, email=email, password=password)
db.session.add(user)
db.session.commit()

更新:找到了问题的答案。函数add_dir_to_db((的db.drop_all((没有任何绑定键集。对于默认数据库,我只需要设置bind_key=None

def add_dir_to_db(dirpath, append=True):
print('Adding Images to Database')
if not append:
db.drop_all(bind_key=None)
db.create_all(bind_key=None)

相关内容

  • 没有找到相关文章

最新更新