Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
- 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()
- 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()
- 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])
Add Comment
Please, Sign In to add comment