Created
January 6, 2016 20:43
-
-
Save tsauerwein/d9630336731fff0547ba to your computer and use it in GitHub Desktop.
SQLAlchemy bulk insertion with None
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sqlalchemy import Column, String, Integer | |
from sqlalchemy.ext.declarative import declarative_base | |
from sqlalchemy import create_engine | |
from sqlalchemy.orm import sessionmaker | |
from sqlalchemy.sql.expression import null | |
Base = declarative_base() | |
class Doc(Base): | |
__tablename__ = 'doc' | |
id = Column(Integer, primary_key=True) | |
col_a = Column(String) | |
col_b = Column(String) | |
engine = create_engine('sqlite:///', echo=True) | |
DBSession = sessionmaker() | |
DBSession.configure(bind=engine) | |
Base.metadata.drop_all(engine, checkfirst=True) | |
Base.metadata.create_all(engine, checkfirst=True) | |
session = DBSession() | |
session.bulk_insert_mappings(Doc, [ | |
dict(id=1, col_a='A', col_b='B'), | |
dict(id=2, col_a='A', col_b=None), | |
dict(id=3, col_a='A', col_b='B') | |
]) | |
session.commit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment