Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Andy Hughes8,113 Points
Stuck on error with ForeignKeyField
I am trying to follow along with the post model steps for the Social App. I'm doing it in VS Code because I want the benefits of more up-to-date code and extension. So far so good, but I've hit an error I can't seem to figure out.
Firstly let me post the error message:
Traceback (most recent call last): File "app.py", line 8, in <module> import forms File "D:\Dropbox\Coding Projects\#######\forms.py", line 6, in <module> from models import User File "D:\Dropbox\Coding Projects\#######\models.py", line 45, in <module> class Post(Model): File "D:\Dropbox\Coding Projects\#######\models.py", line 47, in Post user = ForeignKeyField( TypeError: __init__() missing 1 required positional argument: 'model'
If I'm correct, the error is referring to the ForeignKeyField in my Post class? I have rewatched the videos, checked the syntax, scoured stackoverflow and looked at the WTForms & Flask docs. But I can't seem to find what's wrong. Hence the post.
Below is my code that seems to be throwing the error:
class Post(Model): timestamp = DateTimeField(default=datetime.datetime.now) user = ForeignKeyField( rel_model=User, related_name='posts' ) content = TextField() class Meta: database = DATABASE order_by = ('-timestamp',)
What I can say, is that if I comment out the user variable part, the app then loads and runs as expected (up to the part where it needs to submit the post.).
Part of me is wondering whether this is a database issue. It seems to me like maybe the form is not able to submit the post because it doesn't have the relevant tables to post too.
The below piece of code obviously creates the User table but how is the Post table created to store the posts?
def initialize(): DATABASE.connect() DATABASE.create_tables([User], safe=True) DATABASE.close()
Any clues or help would be greatly appreciated. :)
Chris FreemanTreehouse Moderator 67,982 Points
Hey Andy Hughes, thanks for including the traceback. It holds the vital clue.
The trackbacks points to models.py, line 47 within Post definition.
The error is a missing positional argument. This means there is a parameter that must come before all keyword arguments and is not the name of any provided keyword argument.
A quick check of the documentation shows a model argument is missing. Perhaps,
Could it be that
rel_model=User should just be
Post back if you need more help. Good luck!!!