Skip to content

Commit

Permalink
fix table initialization sequence problem
Browse files Browse the repository at this point in the history
  • Loading branch information
chfw committed May 18, 2015
1 parent b76b298 commit bdbf7c2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
11 changes: 4 additions & 7 deletions doc/source/sqlalchemy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ Now suppose we have these more complex tables:
>>> from sqlalchemy import ForeignKey, DateTime
>>> from sqlalchemy.orm import relationship, backref
>>> import sys
>>> PY33 = (sys.version_info[0] == 3 and sys.version_info[1] == 3)
>>> class Post(Base):
... __tablename__ = 'post'
... id = Column(Integer, primary_key=True)
Expand All @@ -101,8 +100,6 @@ Now suppose we have these more complex tables:
... pub_date = datetime.utcnow()
... self.pub_date = pub_date
... self.category = category
... if PY33:
... self.category_id = category.id
...
... def __repr__(self):
... return '<Post %r>' % self.title
Expand Down Expand Up @@ -160,10 +157,10 @@ Here's the code to update both:
... "Category": [Category, data['Category'][0], None, category_init_func],
... "Post": [Post, data['Post'][0], None, post_init_func]
... }
>>> to_store = {
... "Category": data['Category'][1:],
... "Post": data['Post'][1:]
... }
>>> from pyexcel_io import OrderedDict
>>> to_store = OrderedDict()
>>> to_store.update({"Category": data['Category'][1:]})
>>> to_store.update({"Post": data['Post'][1:]})
>>> save_data(DB_SQL, to_store, session=mysession, tables=tables)

Let's verify what do we have in the database:
Expand Down
14 changes: 5 additions & 9 deletions tests/test_sql_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
DB_SQL,
SQLBookReader,
SQLBookWriter,
from_query_sets
from_query_sets,
OrderedDict
)
from pyexcel_io.sqlbook import SQLTableReader, SQLTableWriter
from sqlalchemy.orm import relationship, backref
from nose.tools import raises
import sys
PY33 = (sys.version_info[0] == 3 and sys.version_info[1] == 3)


engine=create_engine("sqlite:///tmp.db")
Expand Down Expand Up @@ -45,8 +44,6 @@ def __init__(self, title, body, category, pub_date=None):
pub_date = datetime.utcnow()
self.pub_date = pub_date
self.category = category
if PY33:
self.category_id = category.id

def __repr__(self):
return '<Post %r>' % self.title
Expand Down Expand Up @@ -215,10 +212,9 @@ def post_init_func(row):
"Category": [Category, data['Category'][0], None, category_init_func],
"Post": [Post, data['Post'][0], None, post_init_func]
}
to_store = {
"Category": data['Category'][1:],
"Post": data['Post'][1:]
}
to_store = OrderedDict()
to_store.update({"Category": data['Category'][1:]})
to_store.update({"Post": data['Post'][1:]})
writer = SQLBookWriter(DB_SQL,
session=self.session,
tables=tables)
Expand Down

0 comments on commit bdbf7c2

Please sign in to comment.