Created
January 11, 2020 09:20
-
-
Save binarybrat/058b7038071f259f9e84911ccfb6aadd to your computer and use it in GitHub Desktop.
issues with trying to insert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from sqlalchemy import Column, ForeignKey, String, create_engine, Text, Integer, DateTime, Table | |
| from sqlalchemy.orm import relationship, sessionmaker | |
| from sqlalchemy.ext.declarative import declarative_base | |
| import datetime | |
| engine = create_engine('mysql+pymysql://riotdorque:*******@****:3306/patternHelperTests', echo=True) | |
| Base = declarative_base() | |
| metadata = Base.metadata | |
| Session = sessionmaker(bind=engine) | |
| session = Session() | |
| pattern_posts_table = Table('pattern_posts', Base.metadata, | |
| Column('pattern_id', Integer, ForeignKey('patterns.id')), | |
| Column('post_id', Integer, ForeignKey('pending_posts.id'))) | |
| class Pattern(Base): | |
| __tablename__ = 'patterns' | |
| id = Column(Integer, primary_key=True) | |
| patid = Column(String(255), unique=True) | |
| company = Column(String(255)) | |
| patname = Column(String(255)) | |
| details = Column(Text) | |
| price = Column(String(50)) | |
| skills = Column(String(255)) | |
| fabric = Column(Text) | |
| notions = Column(String(255)) | |
| sizing = Column(String(255)) | |
| url = Column(String(255)) | |
| processed = Column(Integer, default=0) | |
| posts = relationship('PendingPost', secondary=pattern_posts_table, back_populates='posts') | |
| def __repr__(self): | |
| return "<Pattern(id='%s', patid='%s', patname='%s')>" % ( | |
| self.id, self.patid, self.patname) | |
| class ImageSet(Base): | |
| __tablename__ = 'imageurls' | |
| id = Column(Integer, primary_key=True) | |
| pid = Column(Integer, ForeignKey('patterns.id')) | |
| url = Column(String(255)) | |
| pattern = relationship("Pattern", back_populates="imageurls") | |
| def __repr__(self): | |
| return "<Image(imgurl='%s')>" % (self.url) | |
| Pattern.imageurls = relationship("ImageSet", order_by=ImageSet.id, back_populates="pattern") | |
| class PendingPost(Base): | |
| __tablename__ = 'pending_posts' | |
| id = Column(Integer, primary_key=True) | |
| created = Column(DateTime, nullable=False) | |
| idstr = Column(String(25), nullable=False, unique=True) | |
| author = Column(String(120)) | |
| title = Column(Text(collation='utf8mb4_unicode_ci')) | |
| link = Column(Text(collation='utf8mb4_unicode_ci')) | |
| flair = Column(String(150, 'utf8mb4_unicode_ci')) | |
| processed = Column(Integer) | |
| comidstr = Column(String(120, 'utf8mb4_unicode_ci')) | |
| body = Column(Text(collation='utf8mb4_unicode_ci')) | |
| patterns = relationship('Pattern', secondary=pattern_posts_table, back_populates='patterns') | |
| def __repr__(self): | |
| return "<Post(idstr='%s', author='%s', title='%s')>" % ( | |
| self.idstr, self.author, self.title) | |
| # After importing everything from this file, and running: Base.metadata.create_all(engine) and bringing in my input data I try: | |
| # | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from postrelationshiptesting import * | |
| Base.metadata.create_all(engine) | |
| ### Here is where the trackback gets thrown... | |
| p1 = Pattern(patid=pdata['patid'], company=pdata['company'], patname=pdata['patname'], details=pdata['details'], price=pdata['price'], skills=pdata['skills'], fabric=pdata['fabric'], notions=pdata['notions'], sizing=pdata['sizing'], url=pdata['url']) | |
| ### Traceback - | |
| Traceback (most recent call last): | |
| File "C:\Users\ri0td_000\AppData\Local\Programs\Python\Python36-32\lib\site-packages\sqlalchemy\orm\mapper.py", line 2043, in get_property | |
| return self._props[key] | |
| KeyError: 'posts' | |
| During handling of the above exception, another exception occurred: | |
| Traceback (most recent call last): | |
| File "<input>", line 1, in <module> | |
| File "<string>", line 2, in __init__ | |
| File "C:\Users\ri0td_000\AppData\Local\Programs\Python\Python36-32\lib\site-packages\sqlalchemy\orm\instrumentation.py", line 376, in _new_state_if_none | |
| state = self._state_constructor(instance, self) | |
| File "C:\Users\ri0td_000\AppData\Local\Programs\Python\Python36-32\lib\site-packages\sqlalchemy\util\langhelpers.py", line 855, in __get__ | |
| obj.__dict__[self.__name__] = result = self.fget(obj) | |
| File "C:\Users\ri0td_000\AppData\Local\Programs\Python\Python36-32\lib\site-packages\sqlalchemy\orm\instrumentation.py", line 202, in _state_constructor | |
| self.dispatch.first_init(self, self.class_) | |
| File "C:\Users\ri0td_000\AppData\Local\Programs\Python\Python36-32\lib\site-packages\sqlalchemy\event\attr.py", line 322, in __call__ | |
| fn(*args, **kw) | |
| File "C:\Users\ri0td_000\AppData\Local\Programs\Python\Python36-32\lib\site-packages\sqlalchemy\orm\mapper.py", line 3360, in _event_on_first_init | |
| configure_mappers() | |
| File "C:\Users\ri0td_000\AppData\Local\Programs\Python\Python36-32\lib\site-packages\sqlalchemy\orm\mapper.py", line 3248, in configure_mappers | |
| mapper._post_configure_properties() | |
| File "C:\Users\ri0td_000\AppData\Local\Programs\Python\Python36-32\lib\site-packages\sqlalchemy\orm\mapper.py", line 1947, in _post_configure_properties | |
| prop.init() | |
| File "C:\Users\ri0td_000\AppData\Local\Programs\Python\Python36-32\lib\site-packages\sqlalchemy\orm\interfaces.py", line 196, in init | |
| self.do_init() | |
| File "C:\Users\ri0td_000\AppData\Local\Programs\Python\Python36-32\lib\site-packages\sqlalchemy\orm\relationships.py", line 1917, in do_init | |
| self._generate_backref() | |
| File "C:\Users\ri0td_000\AppData\Local\Programs\Python\Python36-32\lib\site-packages\sqlalchemy\orm\relationships.py", line 2202, in _generate_backref | |
| self._add_reverse_property(self.back_populates) | |
| File "C:\Users\ri0td_000\AppData\Local\Programs\Python\Python36-32\lib\site-packages\sqlalchemy\orm\relationships.py", line 1848, in _add_reverse_property | |
| other = self.mapper.get_property(key, _configure_mappers=False) | |
| File "C:\Users\ri0td_000\AppData\Local\Programs\Python\Python36-32\lib\site-packages\sqlalchemy\orm\mapper.py", line 2046, in get_property | |
| "Mapper '%s' has no property '%s'" % (self, key) | |
| sqlalchemy.exc.InvalidRequestError: Mapper 'mapped class PendingPost->pending_posts' has no property 'posts' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment