this is the 0.3 syntax, and now it works.
class MyType(TypeDecorator):
impl = Numeric
def convert_bind_param(self, value, engine):
return decimal.Decimal(str(value))
def convert_result_value(self, value, engine):
return decimal.Decimal(str(value))
Thank you very much, Michael. :-)
j
jo wrote:
Michael Bayer wrote:
jose soares wrote:
Michael Bayer wrote:
jose soares wrote:
Hi all,
I'm using Oracle and PostgreSQL with SQLAlchemy and I have some
troubles
to make the code compatible with both of them.
Numeric sa type returns a different type with oracle and pg.
For example, in the following table I'm using the Column 'importo' with
type Numeric as:
tbl['prestazione'] = Table('prestazione', database.metadata,
Column('id', Integer, Sequence('prestazione_seq'),
nullable=False, primary_key=True),
Column('data', Date, nullable=False),
Column('quantita', Numeric(15,3)),
Column('importo', Numeric(12,3))
)
while oracle returns a float type as:
prestazione.c.importo = 12.0
postgres returns a Decimal type as:
prestazione.c.importo = Decimal("0.000")
and I have difficulties to make code compatible, because sometimes the
program raises a TypeError error as:
TypeError: unsupported operand type(s) for +: 'Decimal' and 'float'
Is there some w.a. to avoid this thing?
if you're on 0.6 (or even 0.5 for that matter) the Numeric type should
be
returning Decimal in all cases from result sets.
I'm using 0.3.10 , I know I must to upgrade but it's not so easy. :-[
Is there something that I can do to avoid this error in 0.3.10?
if its 0.3 probably use TypeDecorator.
Your suggest is fine, Michael,
I tried this...but...
class MyType(types.TypeDecorator):
impl = types.Numeric
def process_bind_param(self, value, dialect):
return decimal.Decimal(str(value))
def process_result_value(self, value, dialect):
return decimal.Decimal(str(value))
def copy(self):
return MyType(self.impl.length)
.....
test = Table('test', database.metadata,
Column('id', Integer, nullable=False, primary_key=True),
Column('data', Date, nullable=False),
Column('importo', MyType(12,3))
)
....
insert into test(data,id,importo) values ('2009-01-01',2,32.331)
select * from test
id | data | importo
-- + ------------------- + -------
2 | 2009-01-01 00:00:00 | 32.331
$tg_admin shell
In [36]: test
Out[36]:
Table('test',ThreadLocalMetaData(),Column('id',Integer(),primary_key=True,nullable=False),Column('data',Date(),nullable=False),Column('importo',MyType()),schema=None)
In [37]:
type(dict(test.select(test.c.id==2).execute().fetchone())['IMPORTO'])
SELECT test.id, test.data, test.importo
FROM test
WHERE test.id = :test_id
{'test_id': 2}
Out[37]: <type 'float'>
---------------------------------
What's wrong?
j
--
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.
--
Jose Soares
Sferacarta Net
Via Bazzanese 69
40033 Casalecchio di Reno
Bologna - Italy
Ph +39051591054
fax +390516131537
web:www.sferacarta.com
Le informazioni contenute nella presente mail ed in ogni eventuale file
allegato sono riservate e, comunque, destinate esclusivamente alla persona o
ente sopraindicati, ai sensi del decreto legislativo 30 giugno 2003, n. 196. La
diffusione, distribuzione e/o copiatura della mail trasmessa, da parte di
qualsiasi soggetto diverso dal destinatario, sono vietate. La correttezza,
l’integrità e la sicurezza della presente mail non possono essere garantite. Se
avete ricevuto questa mail per errore, Vi preghiamo di contattarci
immediatamente e di eliminarla. Grazie.
This communication is intended only for use by the addressee, pursuant to
legislative decree 30 June 2003, n. 196. It may contain confidential or
privileged information. You should not copy or use it to disclose its contents
to any other person. Transmission cannot be guaranteed to be error-free,
complete and secure. If you are not the intended recipient and receive this
communication unintentionally, please inform us immediately and then delete
this message from your system. Thank you.
--
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.