Hi!

On Tue, Feb 07, 2017 at 12:34:36AM +0100, janemba <cap.jane...@gmail.com> wrote:
> Hi,
> 
> I'm experiencing an issue with ForeignKey. First of all, all my primary
> keys use idName attribute as classes use a different name. So I built
> the following classes :
> 
> ------
> 
> class User(sqlobject.SQLObject):
> 
>     class sqlmeta:
>         table  = 'tbl_user'
>         idName = 'user_id'
>         idType = int

   By default idType is int so you don't need it. But you can do it if
you want to be extra explicit.

>     user_firstname  = sqlobject.StringCol(length=45)
>     user_lastname   = sqlobject.StringCol(length=45)
>     user_email      = sqlobject.StringCol(length=45)
>     user_phone      = sqlobject.StringCol(length=20)
>     user_password   = sqlobject.StringCol(length=128)
>     user_city       = sqlobject.StringCol(length=45)
>     user_country    = sqlobject.StringCol(length=45)

   Please remove anything that's not related to your question. Too much
information could be worse than too little.

>     user_section  = sqlobject.ForeignKey('Section')
> 
> class Section(sqlobject.SQLObject):
> 
>     class sqlmeta:
>         table  = 'tbl_section'
>         idName = 'section_id'
>         idType = int
> 
>         section_name = sqlobject.StringCol(length=128)
> 
> ------
> 
> 
> And then the debug output :
> 
>  1/QueryOne:  SELECT user_firstname, user_lastname, user_email,
> user_phone, user_password, user_city, user_country, user_section_id FROM
> tbl_user WHERE ((tbl_user.user_id) = (1))
> 
> As you can see the column "user_section_id" has been generated by
> SQLObject. But that column doesn't exists. It should be "user_section"
> from table "tbl_user". I tried with the attribute "name" with ForeignKey
> method but the requests remain the same.
> 
> Do you know how to fix that ?

   'name' in SQLObject is a pythonic name of column; if you want to
provide a database name it's called 'dbName':

class User(sqlobject.SQLObject):

    class sqlmeta:
        table  = 'tbl_user'
        idName = 'user_id'

    user_section = sqlobject.ForeignKey('Section', dbName='user_section')

   Also you have to know that translation from names to dbName's is
performed by an instance of Style. See styles.py and
tests/test_style.py. If you don't want to submit to default SQLObject
style (which, among other things, adds '_id' to ForeignKey's) and don't
want to provide dbName for every column you can create your own Style
and override the default.

> Cheers,

Oleg.
-- 
     Oleg Broytman            http://phdru.name/            p...@phdru.name
           Programmers don't die, they just GOSUB without RETURN.

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

Reply via email to