我使用peewee
模块来管理Sqlite
数据库中的数据。我的用例场景是我将创建一个包含某些字段的数据库。我还需要在特定时间将列添加到现有数据库中。下面是我应该按预期工作的代码:
from peewee import *
import os
from playhouse.migrate import *
my_db = SqliteDatabase('my_database.db')
migrator = SqliteMigrator(my_db)
class FirstTable(Model):
first_name = CharField(null=True)
last_name = CharField(null=True)
class Meta:
database = my_db
class Checkit:
def __init__(self):
self.db = my_db
self.migrator = migrator
def makeDatabse(self):
if os.path.exists("my_database.db"):
print "File Exists remove it"
os.remove("my_database.db")
try:
self.db.connect()
self.db.create_tables([FirstTable,])
except OperationalError:
print "Table Exists"
def insertDatas(self):
with self.db.atomic():
for i in range(10):
first_name_ = "Hello " + str(i)
last_name_ = "World " + str(i)
db_ = FirstTable(first_name=first_name_, last_name = last_name_)
db_.save()
def alterDatabase(self, columns):
with self.db.transaction():
columnField = CharField(null=True)
for column in columns:
migrate(migrator.add_column("firsttable", column, columnField))
def insertAfterAlteringDatabase(self):
with self.db.atomic():
for i in range(20,30):
first_name_ = "Hello " + str(i)
last_name_ = "World " + str(i)
address_ = "Address " + str(i)
db_ = FirstTable(first_name=first_name_, last_name = last_name_, address=address_)
db_.save()
ch = Checkit()
ch.makeDatabse()
ch.insertDatas()
ch.alterDatabase(["address"])
ch.insertAfterAlteringDatabase()
在为address
添加新列null=True
之后,我正在对更改的数据库进行一些插入。我希望将地址数据看到address
字段,但我没有得到任何这些数据。相反,它是NULL
。我的代码应该运行正常,但它没有按预期工作。问题是什么?
答案 0 :(得分:2)
在<configuration><location path="http://www.example.com/page.asp?id=11"><system.webServer><httpRedirect enabled="true" destination="http://www.anothersite.com" httpResponseStatus="Permanent" /></system.webServer></location></configuration>
中,您需要将新字段添加到模型中。迁移器将列添加到数据库表中,但它未将字段添加到模型类。为此,您可以:
insertAfterAlteringDatabase