no, I didn't dive into sqlalchemy to read the source code

2016-04-16 6:41 GMT+08:00 Bo Yang <tebor...@google.com>:

> Do you know what kind of interface the custom class needs to provide in
> order to use sqlalchemy?
>
> On Tue, Apr 12, 2016 at 8:57 PM GoldenBull Chen <goldenb...@gmail.com>
> wrote:
>
>> I have an existing table in Oracle:
>> --------------------------------------------------------
>> CREATE TABLE test_tbl
>> (
>>    id     NUMBER,
>>    name   VARCHAR2 (100),
>>    val    NUMBER,
>>    CONSTRAINT test_tbl_pk PRIMARY KEY (id)
>> );
>> ---------------------------------------------------------
>>
>>
>> In python, I tried to load the table using sqlalchemy, and everything works 
>> fine:
>>
>> ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>>
>> # -*- encoding: utf8 -*-
>>
>> from pbtest_pb2 import *
>> from sqlalchemy import *
>> from sqlalchemy.orm import *
>>
>> metadata = MetaData()
>>
>> tbl = Table('TEST_TBL', metadata,
>>             Column('id', Integer, primary_key=True),
>>             Column('name', String(100)),
>>             Column('val', Numeric),
>>             )
>>
>>
>> class TestType2(object):
>>     pass
>>
>>
>> t = TestType2 # note here, mapped to a custom class
>>
>> mapper(t, tbl)
>> engine = create_engine('oracle+cx_oracle://user:passwd@machine/orcl')
>> Session = sessionmaker(bind=engine)
>> session = Session()
>> for i in session.query(t):
>>     print(i.id, i.name, i.val)
>>
>> ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>>
>>
>>
>> And here is the corresponding protobuf definition:
>> ---------------------------------------------------------
>> syntax = "proto3";
>>
>>
>> message TestType
>> {
>>   int32 id = 1;
>>   string name = 2;
>>   double val = 3;
>> }
>>
>> ---------------------------------------------------------
>>
>>
>>
>> Now I want to re-use the class generated by proto compiler, so I change
>> the class used in mapping:
>> ---------------------------------------------------------
>>
>> t = TestType # now mapped to the generated class
>>
>> ---------------------------------------------------------
>>
>>
>> Then the piece of code failed with error:
>>
>> ---------------------------------------------------------
>>
>> Traceback (most recent call last):
>>   File "D:/test/pbtest.py", line 22, in <module>
>>     mapper(t, tbl)
>>   File "<string>", line 2, in mapper
>>   File "C:\Anaconda2\lib\site-packages\sqlalchemy\orm\mapper.py", line 629, 
>> in __init__
>>     self._configure_pks()
>>   File "C:\Anaconda2\lib\site-packages\sqlalchemy\orm\mapper.py", line 1223, 
>> in _configure_pks
>>     (self, self.mapped_table.description))
>> sqlalchemy.exc.ArgumentError: Mapper Mapper|TestType|TEST_TBL could not 
>> assemble any primary key columns for mapped table 'TEST_TBL'
>> ---------------------------------------------------------
>>
>>
>> Is it possible to reuse the generated class with sqlalchemy?
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Protocol Buffers" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to protobuf+unsubscr...@googlegroups.com.
>> To post to this group, send email to protobuf@googlegroups.com.
>> Visit this group at https://groups.google.com/group/protobuf.
>> For more options, visit https://groups.google.com/d/optout.
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to