AttributeError: 'function' object has no attribute when querying using join

Viewed 535

I'd like to join query with calendarID across the tables to see if given the (user) ID in channel table, is the channelID of calendarID empty? if so, write to it.

Models.py:

class User(db.Model):
    id = Column(Integer, primary_key=True)
    calendarID = Column(String(100), unique=False, nullable = True)
    Channel = relationship('Channel', backref='owner')
class Channel(db.Model):  
    id = Column(Integer, primary_key=True)
    calendarID = Column(String(100), unique=False, nullable = True)
    channelID = Column(String(100), unique=False, nullable = True)
    owner_id = Column(Integer, ForeignKey('user.id'))

Routes.py:

def activateChannel(id):
    """
    reactivates the channel for notification to be received
    """
    import random
    import string
    breakpoint()
    user = db.session.query(User, Channel).filter(User.id==Channel.id).filter(User.calendarID==Channel.calendarID)
    if user.channelID is None:
        user.channelID = ''
    else:
        pass

However, I get:

*** AttributeError: 'User' object has no attribute 'channelID'
2 Answers

The name channelID is defined inside the Channel class. Where in the "User" class, and hence in the "User" objects, its name is "Channel". So your code should be ...

if user.Channel is None:
        user.Channel = ''

Side note: I think unique is False by default, so there's no need to do it explicitly. Also, it's a convention to name attributes with lowercase, "user.channel"

Get all channels for a given user (by user_id) where the calendarID is not set:

q = session.query(Channel).join(User).filter(User.id == id).filter(Channel.calendarID == None)
Related