sqlacodegen bug ?

338 views
Skip to first unread message

Jean-Luc Menut

unread,
May 24, 2016, 12:35:43 PM5/24/16
to sqlalchemy
Hello,

I'm running sqlcodegen on a database, but the output code seems  buggy.

Most of the tables are converted as class in the code such as :




class FilterReceiver(Base):
    __tablename__
= 'filter_receiver'
    __table_args__
= (
       
ForeignKeyConstraint(['id_item', 'id_attribute'], [u'item_attribute.id_item', u'item_attribute.id_attribute']),
       
Index('idx_filter_receiver', 'id_item', 'id_attribute')
   
)


    id
= Column(Integer, primary_key=True, server_default=text("nextval('filter_receiver_id_seq'::regclass)"))
    id_item
= Column(Integer)
    id_attribute
= Column(Integer)
    id_receiver_type
= Column(ForeignKey(u'receiver_type.id'), index=True)


    item_attribute
= relationship(u'ItemAttribute')
    receiver_type
= relationship(u'ReceiverType')






But some of them are converted as variables, such as : 

t_receiver_type = Table(
   
'receiver_type', metadata,
   
Column('id', Integer, unique=True),
   
Column('name', String(20), unique=True),
   
Column('igs_defined', String(1)),
   
Column('model', String(50))
)



And when I try to create an object related to any table, for example: 

import db
a
= db.Country()

I get this error message: 

When initializing mapper Mapper|FilterReceiver|filter_receiver, expression u'ReceiverType' failed to locate a name ("name 'ReceiverType' is not defined"). If this is a class name, consider adding this relationship() to the <class 'db.FilterReceiver'> class after both dependent classes have been defined.


Am I doing something wrong ?

For information I'm using  sqlacodegen 1.1.6 and SQLAlchemy 1.0.13
the command used to generate the python file is 
sqlacodegen postgresql://USERNAME:PASS...@127.0.0.1/titi --outfile db.py


Thanks by advance for any information and advice

Jean-Luc Menut


Kevin Ernst

unread,
Jun 14, 2017, 11:30:45 PM6/14/17
to sqlalchemy
Hi Jean-Luc,

I wish I'd seen your post earlier, I could've helped. :) I had this exact same problem today, found your post by searching for "sqlacodegen table class."

Something in this semi-unrelated post by Mike Bayer prompted me to check to see what was different about the tables for which proper classes were generated and the others. I found that they were missing a primary key; adding one and re-running sqlacodegen did indeed solve my problem. For tables lacking primary keys, sqlacodegen simply creates a Table variable, rather than a class as you would expect.

Hopefully this can still help someone else in this situation, who comes here searching for a resolution.

Cheers,
Kevin

Sebastian Cheung

unread,
Jul 17, 2021, 10:26:34 AM7/17/21
to sqlalchemy
Also, why only public.* tables are generated in my case, but all non-public.* tables are not generated?
Screenshot 2021-07-17 at 15.26.23.png
Reply all
Reply to author
Forward
0 new messages