[sqlalchemy] Re: Required properties of first arg to bulk_insert_mappings

2017-11-20 Thread Skip Montanaro
I've narrowed down my problem space a bit. Consider this simple code:

from sqlalchemy import (Integer, String, Column, MetaData, create_engine)
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

METADATA = MetaData()
BASE = declarative_base(metadata=METADATA)
SESSION = sessionmaker()

class User(BASE):
__tablename__ = "user"
first_name = Column(String(32))
last_name = Column(String(32))
id = Column(Integer, primary_key=True)

print(type(User))
print(type(METADATA.tables['user']))

When run, I get this output:




The User class is suitable to use as the first arg to 
session.bulk_insert_mapping(), but the object plucked from the METADATA 
tables dict is not. Will I have to always carry around my own references to 
the various subclasses of BASE which I defined to describe my schema? If I 
have metadata and session objects, is there a way to get back that usable 
(or a usable) class?

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] Required properties of first arg to bulk_insert_mappings

2017-11-20 Thread Skip Montanaro
I'm trying to insert a list of dictionaries into a table using 
bulk_insert_mappings(). When I call it, I get this error:

AttributeError: 'Table' object has no attribute 'mapper'

I'm having trouble (in general) figuring out which of the various ways of 
specifying my table(s) are appropriate. I've been doing this (paraphrasing 
because I can't copy out of my work env):

import sqlalchemy as sa
import sa.orm

SESSION = sa.orm.sessionmaker()
engine = sa.create_engine(...)
metadata = sa.MetaData(engine)
session = SESSION(bind=engine)
mytable = sa.Table("mytable", metadata, sa.Column(...), ...)

The session object is created within a contextmanager as described here:

http://docs.sqlalchemy.org/en/latest/orm/session_basics.html

It's within the scope of that contextmanager (a couple levels down in the 
function call stack) that the call is made:

mytable = metadata.tables['mytable']
session.bulk_insert_mappings(mytable, records)

I clearly must not understand something about the parameter properties 
required of that function. The docs state:

*mapper* – a mapped class, or the actual Mapper 

 object, representing the single kind of object represented within the 
mapping list.

I thought I had assembled all the bits (engine, session, bound metadata). 
What's not mapped about my Table object?

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.