0

I would like to chain-connect(? don't know if it is a term) 3 table models like shown:

class User(db.Model, UserMixin):     
    id = db.Column(db.Integer, primary_key = True)     
    username = db.Column(db.String(15), unique = True, nullable = False)     
    password = db.Column(db.String(60), nullable = False)     
    catalogs = db.relationship('UserCatalogs', backref = 'owner', lazy = True)

class UserCatalogs(db.Model):     
    id = db.Column(db.Integer, primary_key = True)        
    title = db.Column(db.String(20), nullable = False)     
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable = False)
    child_cat = db.relationship('ChildCatalog', backref = 'parentcatalog', lazy = True)

class ChildCatalog(db.Model):       
    id = db.Column(db.Integer, primary_key = True)       
    item_id = db.Column(db.Integer, nullable = True)       
    catalog_id = db.Column(db.Integer, db.ForeignKey('usercatalogs.id'), nullable = True)

But when I construct my tables this way, later on in my application I can not execute this block of code successfully:

item_id = request.values.get('item_id') 
catalog_id = request.values.get('catalog_id') 
if (item_id is not None and item_id.isnumeric()) and (catalog_id is not None and catalog_id.isnumeric()):       
childcatalog = ChildCatalog(item_id=int(item_id), parentcatalog=int(catalog_id))
db.session.add(childcatalog) 
db.session.commit()

The error I'm getting is "AttributeError: 'int' object has no attribute '_sa_instance_state'" and I've found this explanation on Stackoverflow here and I'm trying to make it work with 'current_user' function of flask_login but I don't know how to make it work.

Here is the full traceback:

Traceback (most recent call last):

  File "C:\Users\ezell\Anaconda3\lib\site-packages\flask\app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\ezell\Anaconda3\lib\site-packages\flask\app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Users\ezell\Anaconda3\lib\site-packages\flask\app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\ezell\Anaconda3\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "C:\Users\ezell\Anaconda3\lib\site-packages\flask\app.py", line 2446, in wsgi_app
    return manager.original_init(*mixed[1:], **kwargs)
  File "C:\Users\ezell\Anaconda3\lib\site-packages\sqlalchemy\ext\declarative\base.py", line 842, in _declarative_constructor
    setattr(self, k, kwargs[k])
  File "C:\Users\ezell\Anaconda3\lib\site-packages\sqlalchemy\orm\attributes.py", line 268, in __set__
    instance_state(instance), instance_dict(instance), value, None
  File "C:\Users\ezell\Anaconda3\lib\site-packages\sqlalchemy\orm\attributes.py", line 1003, in set
    value = self.fire_replace_event(state, dict_, value, old, initiator)
  File "C:\Users\ezell\Anaconda3\lib\site-packages\sqlalchemy\orm\attributes.py", line 1026, in fire_replace_event
    state, value, previous, initiator or self._replace_token
  File "C:\Users\ezell\Anaconda3\lib\site-packages\sqlalchemy\orm\attributes.py", line 1435, in emit_backref_from_scalar_set_event
    instance_state(child),
AttributeError: 'int' object has no attribute '_sa_instance_state'

What would be the best way to go around this? Thank you so much in advance for your time and attention.

Ezel
  • 1
  • 2
  • Please show the full traceback. I can't match the error up with the code you've shown – roganjosh Mar 10 '20 at 21:27
  • I've added the full traceback, sorry for the inconvenience! – Ezel Mar 11 '20 at 06:30
  • You have two issues. First, `ChildCatalogs` will create a table called `child_catalogs`, with the underscore. Second, you're trying to pass a named parameter that is a backreference. `childcatalog = ChildCatalog(item_id=int(item_id), catalog_id=int(catalog_id))` – roganjosh Mar 11 '20 at 08:26
  • Thank you so much for the breakdown of my problem! But my catalog_id has to be passed in somehow so that I can save that item_id with that specific catalog_id that is grabbed with request.values.get. I do not know how to achieve this. – Ezel Mar 11 '20 at 09:00
  • I showed you how to pass `catalog_id`? – roganjosh Mar 11 '20 at 09:03
  • Oh yes I'm extremely sorry, I didn't realize that I've to pass it as catalog_id and not parentcatalog. Thank you again! – Ezel Mar 11 '20 at 09:24

0 Answers0