0

Here are the two classes I've defined.

class Day(db.Model):
    __tablename__ = 'day'
    __table_args__ = {'extend_existing': True}
    id = db.Column(db.Integer, nullable = False, primary_key = True)
    name = db.Column(db.String(9), nullable = False)    
    #Foreign Keys
    function_id = db.Column(db.Integer, db.ForeignKey(Function.id))
    schedule_id = db.Column(db.Integer, db.ForeignKey(Schedule.id)) #Python wont recognize this for some reason...

class Schedule(db.Model):
    __tablename__ = 'schedule'
    __table_args__ = {'extend_existing': True}
    id = db.Column(db.Integer, nullable = False, primary_key = True)
    #Foreign Keys
    assigned_employee = db.Column(db.Integer, db.ForeignKey(Employee.id))    
    #Relationships
    days = db.relationship('Schedule', backref = 'day', lazy = True)

I'm using python in the command prompt to check the code and it seems to not recognize the Schedule class right below it.

Traceback (most recent call last):

File "models.py", line 1, in from main import db
File "Not\The\Actual\Path.py", line 9, in from views import *
File "Not\The\Actual\Path.py", line 2, in from models import *
File "Not\The\Actual\Path.py", line 48, in class Day(db.Model):
File "Not\The\Actual\Path.py", line 57, in Day schedule_id = db.Column(db.Integer, db.ForeignKey(Schedule.id)) #Python wont recognize this for some reason...
NameError: name 'Schedule' is not defined

It's perfectly fine with the 'function_id' line, but angry about the one right below it.

2
  • 1
    If your classes are defined in that order, it's because the interpreter hasn't gotten to class Schedule yet since it comes after class Day. Order matters in Python Commented Mar 17, 2018 at 7:47
  • I am ashamed. I knew c++ was like that, but not python. I thought that since it was a higher level language it wouldn't matter. I guess it makes sense since it's interpreted line-by-line. Every line would by a command in the cli, in a sense. Commented Mar 17, 2018 at 7:55

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.