SQLAlchemy Automap backref errors

I'm new to SQLAlchemy (usually ORM), and I'm trying to port an existing application to SQLAlchemy so that we can port some of the code complexity from existing (and tedious to update) Python queries. Unfortunately, I get errors right after the database reflection. Although I can query tables directly, I do not have direct access to classes or relations between classes. Below is an example of a minimal example of what I'm trying to do.

Current postgres table:

dev=> \d+ gmt_file
                                        Table "public.gmt_file"
  Column   |     Type     | Modifiers | Storage  | Stats target | Description 
-----------+--------------+-----------+----------+--------------+-------------
 file_id   | integer      | not null  | plain    |              |
       a   | integer      |           | plain    |              | 
       b   | integer      |           | plain    |              | 
Indexes:
    "gmt_file_pk" PRIMARY KEY, btree (file_id)
Foreign-key constraints:
    "gmt_file_a_fk" FOREIGN KEY (a) REFERENCES cmn_user(user_id)
    "gmt_file_b_fk" FOREIGN KEY (b) REFERENCES cmn_user(user_id)

SQLAlchemy application (minimal example):

from sqlalchemy import create_engine
from sqlalchemy.orm import Session,Mapper
from sqlalchemy.ext.automap import automap_base

engine = create_engine('postgresql://user:pass@localhost:5432/dev')
Base = automap_base()
Base.prepare(engine, reflect=True)
session = Session(engine,autocommit=True)

session.query(Base.classes.gmt_file).all()

, , backref - , a b ( db). , (name_for_scalar_relationship() name_for_collection_relationship()), . backref SQLAlchemy?

db , , , . .

+4
1

, automap, .

Base.prepare name_for_scalar_relationship name_for_collection_relationship, , . (. AutomapBase.prepare() name_for_collection_relationship()) backref, .

:

from sqlalchemy import create_engine
from sqlalchemy.orm import Session,Mapper
from sqlalchemy.ext.automap import automap_base, name_for_collection_relationship

engine = create_engine('postgresql://user:pass@localhost:5432/dev')
Base = automap_base()

def _name_for_collection_relationship(base, local_cls, referred_cls, constraint):
    if constraint.name:
        return constraint.name.lower()
    # if this didn't work, revert to the default behavior
    return name_for_collection_relationship(base, local_cls, referred_cls, constraint)

Base.prepare(engine, reflect=True, name_for_collection_relationship=_name_for_collection_relationship)
session = Session(engine,autocommit=True)

session.query(Base.classes.gmt_file).all()

, , gmt_file_a_fk gmt_file_b_fk.

. , name_for_scalar_relationship().

, . :

from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy.orm import relationship

class GmtFile(Base):
    __tablename___ = 'gmt_file'

    file_id = Column('file_id', Integer, primary_key=True)

    a = Column('a', Integer, ForeignKey('CmnUser.user_id', name='gmt_file_a'))
    b = Column('b', Integer, ForeignKey('CmnUser.user_id', name='gmt_file_b'))
    # you'll need to define the class CmnUser as well

    # these variable names need to be the same as the ForeignKey names above
    gmt_file_a = relationship('CmnUser', foreign_keys=[a])
    gmt_file_b = relationship('CmnUser', foreign_keys=[b])
+1

All Articles