Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases

Abigail Solomon
seal-mask
.a{fill-rule:evenodd;}techdegree
Abigail Solomon
Data Analysis Techdegree Student 4,440 Points

Name Error: name 'User' is not defined. I cannot add my name to the userbase because of this error.

I'm currently working on the SQLAlchemy course and my file won't update with my name because of this name error, however I copied what Megan did during her demonstration and I'm not sure what could have been wrong. It's supposed to follow the format of the repr(self) function in the models.py, so I'm not sure what went wrong for me. I'm using the Treehouse console. This is the line of code that got the error: abi_user = User(name='Abigail', fullname='Abigail Solomon', nickname='Abi').

Here's the full code:

from sqlalchemy import create_engine, Column, Integer, String from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker

engine = create_engine('sqlite:///users.db', echo=True) Session = sessionmaker(bind=engine) session = Session() Base = declarative_base()

class User(Base): tablename = 'users'

id = Column(Integer, primary_key = True) name = Column(String) fullname = Column(String) nickname = Column(String)

def repr(self): return f'<User(name={self.name}, fullname={self.fullname}, nickname={self.nickname})>'

if name == 'main': Base.metadata.create_all(engine)

abi_user = User(name='Abigail', fullname='Abigail Solomon', nickname='Abi')
print(abi_user.name)
print(abi_user.id)
session.add(abi_user)
print(session.new)

1 Answer

Travis Alstrand
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Travis Alstrand
Treehouse Project Reviewer

It's hard to tell without seeing everything, but if you're getting a User is not defined error, that's in regards to that User class. I would ensure that you're importing it correctly.

If you continue to be stuck on this, could you please provide all the code in this file and a link to the video please?