django1.8 修改数据库 时间字段 遇到非空问题的解决办法
2017-09-16 17:33
537 查看
lmb@lmb-Think:~/bbs02$ python manage.py makemigrations You are trying to add a non-nullable field 'update_time' to tags without a default; we can't do that (the database needs something to populate existing rows). Please select a fix: 1) Provide a one-off default now (will be set on all existing rows) 2) Quit, and let me add a default in models.py Select an option: 1 Please enter the default value now, as valid Python The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now() >>> timezone.now() Migrations for 'web': 0021_tags_update_time.py: - Add field update_time to tags lmb@lmb-Think:~/bbs02$ python manage.py migrate Operations to perform: Synchronize unmigrated apps: staticfiles, messages, ckeditor Apply all migrations: admin, web, contenttypes, auth, sessions Synchronizing apps without migrations: Creating tables... Running deferred SQL... Installing custom SQL... Running migrations: Rendering model states... DONE Applying web.0021_tags_update_time... OK
class Tags(models.Model): ''' 标签表 ''' tagname = models.CharField(u"关键字",max_length=20) num = models.CharField(u"频率",max_length=100) update_time = models.DateTimeField(u"更新时间",auto_now=True) def __unicode__(self): return self.tagname
makemigrations 时遇到错误:
新增 update_time 字段时更新数据库遇到提示如下:(update_time 字段非空字段)
1) Provide a one-off default now (will be set on all existing rows)
2) Quit, and let me add a default in models.py
这时 选择 1
输入 timezone.now()添加当前时间入库
然后执行 migrate 成功。
相关文章推荐
- 使用Linq 更新数据库时遇到的一些问题及解决办法
- django1.8数据库显示时间比本地时间小8个小时怎么解决
- 关于tomcat端口的修改,以及同时部署多个应用,自己遇到的问题以及解决办法
- 关于修改当前WiFi的账号和密码之后自动重连问题(不用在输入密码),重连时间过长问题的解决办法
- windows及linux下安装django simple captcha 遇到的各种问题及解决办法
- Django 使用 MySQL 存储时间中遇到的问题(在数据库中记录插入时间、更新时间、删除时间)
- 使用Linq 更新数据库时遇到的一些问题及解决办法
- django2源码安装xadmin过程中遇到模块缺少,如No module named 'crispy_forms'等问题解决办法
- 关于数据库字段不是主键但是也不能重复的问题解决办法?
- DNS 服务器遇到很多运行时间事件问题解决办法。
- 安装Sql Server 2008,遇到的无权限创建数据库、修改身份验证模式等问题的原因及解决方法
- Win10 系统安装Sql Server2008 R2 数据库遇到的问题及解决办法总结!
- 关于打开现有项目时数据库连接配置遇到的问题 连接字符串中的数据源值指定未安装的SQL Server的实例。要解决此问题,可选择安装匹配的SQL Server实例或修改连接字符串中的数据源值
- oracle 11g数据库安装最强教程及遇到的问题解决办法
- 前段时间做项目中,遇到的问题以及解决办法的总结(一)
- [Django]我的第一个网页,报错啦~(自己实现过程中遇到问题以及解决办法)
- C#中DataGridView绑定了DataTable后,通过代码修改DataGridView中的数据,总有一行(被修改过并被用户选中的行集合中索引为0的行)不能被UpDate回数据库的问题和解决办法
- 解决SQL SERVER 2008数据库表中修改字段后不能保存的问题
- 遇到的问题------SQL Server 附加数据库失败的解决办法(3415错误)
- python笨办法解决zipfile解压会改变文件最后修改时间的问题