Guest User

Untitled

a guest
Jan 16th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. dev=> d+ gmt_file
  2. Table "public.gmt_file"
  3. Column | Type | Modifiers | Storage | Stats target | Description
  4. -----------+--------------+-----------+----------+--------------+-------------
  5. file_id | integer | not null | plain | |
  6. a | integer | | plain | |
  7. b | integer | | plain | |
  8. Indexes:
  9. "gmt_file_pk" PRIMARY KEY, btree (file_id)
  10. Foreign-key constraints:
  11. "gmt_file_a_fk" FOREIGN KEY (a) REFERENCES cmn_user(user_id)
  12. "gmt_file_b_fk" FOREIGN KEY (b) REFERENCES cmn_user(user_id)
  13.  
  14. from sqlalchemy import create_engine
  15. from sqlalchemy.orm import Session,Mapper
  16. from sqlalchemy.ext.automap import automap_base
  17.  
  18. engine = create_engine('postgresql://user:pass@localhost:5432/dev')
  19. Base = automap_base()
  20. Base.prepare(engine, reflect=True)
  21. session = Session(engine,autocommit=True)
  22.  
  23. session.query(Base.classes.gmt_file).all()
  24.  
  25. from sqlalchemy import create_engine
  26. from sqlalchemy.orm import Session,Mapper
  27. from sqlalchemy.ext.automap import automap_base, name_for_collection_relationship
  28.  
  29. engine = create_engine('postgresql://user:pass@localhost:5432/dev')
  30. Base = automap_base()
  31.  
  32. def _name_for_collection_relationship(base, local_cls, referred_cls, constraint):
  33. if constraint.name:
  34. return constraint.name.lower()
  35. # if this didn't work, revert to the default behavior
  36. return name_for_collection_relationship(base, local_cls, referred_cls, constraint)
  37.  
  38. Base.prepare(engine, reflect=True, name_for_collection_relationship=_name_for_collection_relationship)
  39. session = Session(engine,autocommit=True)
  40.  
  41. session.query(Base.classes.gmt_file).all()
  42.  
  43. from sqlalchemy import Column, Integer, ForeignKey
  44. from sqlalchemy.orm import relationship
  45.  
  46. class GmtFile(Base):
  47. __tablename___ = 'gmt_file'
  48.  
  49. file_id = Column('file_id', Integer, primary_key=True)
  50.  
  51. a = Column('a', Integer, ForeignKey('CmnUser.user_id', name='gmt_file_a'))
  52. b = Column('b', Integer, ForeignKey('CmnUser.user_id', name='gmt_file_b'))
  53. # you'll need to define the class CmnUser as well
  54.  
  55. # these variable names need to be the same as the ForeignKey names above
  56. gmt_file_a = relationship('CmnUser', foreign_keys=[a])
  57. gmt_file_b = relationship('CmnUser', foreign_keys=[b])
Add Comment
Please, Sign In to add comment