Problem Statement -
I want to bulk insert a few hundred rows using SQLAlchemy. The schema looks like following
all_scrips_tbl = Table('all_scrips_info', _METADATA,
Column('security_isin', String(16), primary_key=True),
Column('company_name', String(80)),
Column('nse_traded', Boolean, default=False),
Column('nse_start_date', Date,
default=datetime.date(year=2001, day=1, month=1)),
Column('bse_traded', Boolean, default=False),
Column('bse_start_date', Date,
default=datetime.date(year=2001, day=1, month=1)),
)
Now each scrip can either be - nse_traded=True
, bse_traded=True
or both nse_traded=True and bse_traded=True
So I have insert statements like -
For securities that have only nse_traded=True
ins = t.insert().values(security_isin=nstock.isin,
company_name=nstock.name,
nse_traded=True,
nse_start_date=nstart_date,
)
For securites that have only bse_traded=True
-
ins = t.insert().values(security_isin=bstock.isin,
company_name=bstock.name,
bse_traded=True,
bse_start_date=bstart_date)
and correspondingly for nse_traded=True
and bse_traded=True
I'd like to bulk insert those statements. So something like values().compile
with default values from create statement would be very useful so that I can then use the following -
conn.execute(all_scrips_info.insert() , [ {}, {} ] )
Where dicts are populated with defaults as appropriate?
I also looked at this question, but this is slightly different than my requirement. There's an old question on google groups which is similar to my requirement. But the sqlalchemy version there is rather old, plus answers are not very easily understandable.
Am I missing something very obvious?