Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

Fix bug: Precision for Float column which stores timestamp is too small #83

Merged
merged 2 commits into from
Jan 18, 2015

Conversation

piglei
Copy link
Contributor

@piglei piglei commented Jan 18, 2015

Float(16) if not precise enough for storing unix timestamp, this may cause various bugs.
See my script for testing:

# -*- coding: utf-8 -*-
import time
from sqlalchemy import create_engine
from sqlalchemy import Column, Float, MetaData, Table

echo = False

engines = (
    ('postgresql', create_engine('postgresql://DB_CONFIG', echo=echo)),
    ('mysql', create_engine('mysql://DB_CONFIG', echo=echo)),
    ('sqlite', create_engine('sqlite://', echo=echo)),
)

meta_data = MetaData()

test = Table(
    'test', meta_data,
    Column('col16', Float(precision=16)),
    Column('col32', Float(precision=32)),
)

for engine_type, engine in engines:
    print 'Engine:', engine_type
    meta_data.drop_all(engine)
    meta_data.create_all(engine)

    now = time.time()
    print 'Now:', now
    engine.execute('insert into test values (%s, %s)' % (now, now))

    for each in engine.execute(test.select()):
        print 'Float(16): %f Float(32): %f' % tuple(each)

    print 

Output:

Engine: postgresql
Now: 1421572566.87
Float(16): 1421570000.000000 Float(32): 1421572566.870000

Engine: mysql
Now: 1421572566.9
Float(16): 1421570000.000000 Float(32): 1421572566.900000

Engine: sqlite
Now: 1421572566.9
Float(16): 1421572566.900000 Float(32): 1421572566.900000

Only sqlite can give the right result.

@binux
Copy link
Owner

binux commented Jan 18, 2015

I only tested sqlalchemy with sqlite. Thank you!

binux added a commit that referenced this pull request Jan 18, 2015
Fix bug: Precision for Float column which stores timestamp is too small
@binux binux merged commit 58e30fc into binux:master Jan 18, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants