Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

representation/printing of Models fails when model is not yet persisted to the database #554

Closed
jeff-99 opened this issue Oct 4, 2017 · 0 comments
Labels
Milestone

Comments

@jeff-99
Copy link
Contributor

jeff-99 commented Oct 4, 2017

When you create an new instance of a model you cannot print it or use it in logs, because the repr method of Model tries to loop through the identity parameter of inspect(), which is None.

After persisting to the database the instance can be printed.

example:

>>> from application.extensions import db
>>> from application.model.models import User
>>> u = User()
>>> print(u)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/jeff/virtualEnvs/kongoserver/lib/python3.5/site-packages/flask_sqlalchemy/model.py", line 121, in __repr__
    pk = ', '.join(to_str(value) for value in inspect(self).identity)
TypeError: 'NoneType' object is not iterable
>>> u.id = 12345
>>> print(u)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/jeff/virtualEnvs/kongoserver/lib/python3.5/site-packages/flask_sqlalchemy/model.py", line 121, in __repr__
    pk = ', '.join(to_str(value) for value in inspect(self).identity)
TypeError: 'NoneType' object is not iterable
>>> db.session.add(u)
>>> db.session.commit()
>>> print(u)
<User 12345>

Where User is defined like:

class User(db.Model):
    __tablename__ = 'application_user'

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), unique=True)
    password = db.Column(db.String(255))
@davidism davidism added the bug label Oct 4, 2017
@davidism davidism added this to the 2.3.1 milestone Oct 4, 2017
davidism pushed a commit that referenced this issue Oct 4, 2017
Fixed representation of new model instances
Added "transient" id for unpersisted models
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

2 participants