V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Zuckonit
V2EX  ›  Python

sqlalchemy sqlite DateTime default 无效

  •  
  •   Zuckonit · 2014-07-09 09:39:54 +08:00 · 6264 次点击
    这是一个创建于 3578 天前的主题,其中的信息可能已经有所发展或是发生改变。
    #!/usr/bin/env python
    # encoding: utf-8

    from datetime import datetime
    from base import Base

    from sqlalchemy import Column
    from sqlalchemy import DateTime, String, Integer
    from datetime import
    class Info(Base):
    ....__tablename__ = ‘file_info'

    ....id = Column(Integer, primary_key=True, autoincrement=True, nullable=False)
    ....md5 = Column(String(32), nullable=False, index=True)
    ....first_time = Column(DateTime, nullable=False, default=datetime.now)
    ....last_time = Column(DateTime, nullable=False, default=datetime.now)

    通过这个模型建立表后,
    .schema是这样的
    CREATE TABLE tb_sample_info (
    ........id INTEGER NOT NULL,
    ........md5 VARCHAR(32) NOT NULL,
    ........first_time DATETIME,
    ........last_time DATETIME,
    ........PRIMARY KEY (id)
    );
    为啥firt_time, last_time没default时间
    7 条回复    2014-07-15 18:37:29 +08:00
    humiaozuzu
        1
    humiaozuzu  
       2014-07-09 09:43:40 +08:00
    因为是在 ORM 层做的,不是数据库系统中
    Zuckonit
        2
    Zuckonit  
    OP
       2014-07-09 10:00:43 +08:00
    orm层不能做?
    Zuckonit
        3
    Zuckonit  
    OP
       2014-07-09 10:00:54 +08:00
    @humiaozuzu orm层不能做?
    Zuckonit
        4
    Zuckonit  
    OP
       2014-07-09 10:17:19 +08:00
    PS: CREATE TABLE tb_sample_info 这个应该是CREATE TABLE file_info
    Ever
        5
    Ever  
       2014-07-09 10:28:06 +08:00   ❤️ 1
    default是插入时sqlalchemy自己干.
    server_default才是写在sql创建时让db干的.
    Zuckonit
        6
    Zuckonit  
    OP
       2014-07-09 10:36:04 +08:00
    @Ever 赞!
    Zuckonit
        7
    Zuckonit  
    OP
       2014-07-15 18:37:29 +08:00
    @Ever
    通过server_default=func.now()设置为当前时间, 但跟服务器上的时间相差8个小时, why?如何让其一致
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5988 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 02:09 · PVG 10:09 · LAX 19:09 · JFK 22:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.