Hi Mr. Bayer,
On Aug 29, 10:10 am, Michael Bayer <[EMAIL PROTECTED]> wrote:
> On Aug 29, 2008, at 10:53 AM, Mike wrote:
>
>
>
> > If I use pymssql instead, it works. As I understand it, SA should be
> > using pymssql anyway, so I don't know why this is happening. I can
> > cast the unicode to a string, so it's not a big deal. However, I
> > thought someone might want to know that this is happening.
>
> without a sample of the code and schema in use, we can't say for sure
> what the issue is. Are you making usage of the Unicode type at least ?
>
>
Since I am autoloading, I didn't know I could use the Unicode type to
override what was reflected. How do I do that?
There's only so much I can give being that I'm talking about
accounting data, but here's a sample that should work:
<code>
from sqlalchemy import create_engine, Column, Float, Integer
from sqlalchemy import MetaData, String, Table, Unicode
from sqlalchemy.orm import mapper, sessionmaker
class Acct_Prefs(object):
''' Table object for tbl_Acct_Prefs '''
def __init__(self, empID, pref_name, pref_value):
self.empID = empID
self.pref_name = pref_name
self.pref_value = pref_value
def __repr__(self):
return "<Acct_Prefs ('%s', '%s', '%s')>" % (self.empID,
self.pref_name,
self.pref_value)
class TimeEntries(object):
''' Table object for tbl_TimeEntries '''
def __init__(self, dateworked, empid, reg, ot, ce,
hol, sklv, vac, ct, conv, misc,
comments):
self.dateworked = dateworked
self.empid = empid
self.reg = reg
self.ot = ot
self.ce = ce
self.hol = hol
self.sklv = sklv
self.vac = vac
self.ct = ct
self.conv = conv
self.misc = misc
self.comments = comments
def __repr__(self):
return "<TimeEntries ('%s', '%s')>" % (self.dateworked,
self.empid)
# Connect to the database
print 'connecting to MCISAccounting DB...'
engine = create_engine('sqlite:///sample.db')
metadata = MetaData(engine)
# Load the tables
print 'loading tables...'
prefs_table = Table('tbl_Acct_Prefs', metadata,
Column('empID', String(5), primary_key=True),
Column('pref_name', String(50), primary_key=True,
nullable=False),
Column('pref_value', String(50), nullable=False)
)
entry_table = Table('tbl_TimeEntries', metadata,
Column('dateworked', String(10),
primary_key=True),
Column('empid', Integer, primary_key=True,
autoincrement=False),
Column('reg', Float),
Column('ot', Float),
Column('ce', Float),
Column('hol', Float),
Column('sklv', Float),
Column('vac', Float),
Column('ct', Float),
Column('conv', Float),
Column('misc', Float),
Column('comments',
Unicode(256))
)
metadata.create_all()
# Map the tables
print 'mapping tables...'
mapper(TimeEntries, entry_table)
mapper(Acct_Prefs, prefs_table)
# Create a session object
print 'creating session...'
Session = sessionmaker(bind=engine)
session = Session()
prefOne = Acct_Prefs(258, 'last_payPeriod', 2)
session.Add(prefOne)
session.commit()
</code>
That should get it to look as similar as possible to the state the SQL
Server is in. Now here's where I get the issue. I do the following
query
pref = self.session.query(Acct_Prefs).filter_by(empID=258,
pref_name='last_payPeriod').first()
and then I do the following where someValue happens to be a unicode
string:
pref.pref_value = someValue
session.commit()
>
> > Oh, and I use autoload to load my Table() objects. I'm not sure if
> > that's significant or not though. I am also using a session.commit()
> > to get the error above.
>
> You might want to enable convert_unicode=True on your Engine, or
> override the specific columns requiring unicode compatibility with the
> Unicode type. String columns are reflected by default without unicode
> awareness.
This might work. I'll give it a whirl anyway. I hope my code sample
makes sense and isn't too ugly. I'm still pretty green with your cool
ORM and SQL in general.
Mike
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---