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

Error Authenticating a User. #222

Closed
JWTappert opened this issue Dec 21, 2016 · 21 comments
Closed

Error Authenticating a User. #222

JWTappert opened this issue Dec 21, 2016 · 21 comments
Labels

Comments

@JWTappert
Copy link

JWTappert commented Dec 21, 2016

Hello, Miguel I'm working through chapter 8 and I'm getting an error when trying to first authenticate the users. It seems like the tables aren't getting created in my db but when I run python manage.py shell and print u.username or any other of the attributes I get the correct values returned. Perhaps you know where I'm going wrong? Let me know if I can provide you with any other files. Thanks a lot.

OperationalError: (sqlite3.OperationalError) no such table: users [SQL: u'SELECT users.id AS users_id, users.email AS users_email, users.username AS users_username, users.password_hash AS users_password_hash, users.role_id AS users_role_id \nFROM users \nWHERE users.email = ?\n LIMIT ? OFFSET ?'] [parameters: (u'john@example.com', 1, 0)]

@miguelgrinberg
Copy link
Owner

The error says the users table does not exist. Have you updated your database migrations?

@JWTappert
Copy link
Author

JWTappert commented Dec 21, 2016

I've done python manage.py db upgrade. Is that all that is needed to update the database migrations?

@miguelgrinberg
Copy link
Owner

Yes, that should do it. Look at the output of the command to see if there are any errors. And if you run an update a second time it should say that there's nothing to upgrade, that you are already updated.

Any chance the active configuration is different when you do the update versus when you run the server?

@JWTappert
Copy link
Author

JWTappert commented Dec 21, 2016

Nope, no errors after upgrade. I think my migration isn't setup properly actually. This is the message I get when running upgrade (any number of times):

  warnings.warn('SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True to suppress this warning.')
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.

Hmm, I don't think the active config is different. I'm just using default in both cases I believe.

@miguelgrinberg
Copy link
Owner

Hmm. Is this code from this repository without any changes? Are you sure you are not calling db.drop_tables() somewhere? Or maybe the configuration of the database URL is done as a relative path, and you are running the upgrade from a different directory then the one from where you run the server?

From your description of the problem, either the server isn't finding the database (so it creates a new empty one), or the database is in the right place, but something is destroying the tables. Look for database files in all the directories you are using, if you find more than one sqlite file, then maybe it is the relative path problem I described above.

@JWTappert
Copy link
Author

JWTappert commented Dec 21, 2016

There is only one database file. dev-data.sqlite

I don't have db.drop_tables() anywhere but come to think of it I don't think I have db.create_all() anywhere either.

I will review the repo and the database chapter and see if I've missed something. Thank you very much for your help!

@miguelgrinberg
Copy link
Owner

You do not need db.create_all(), Flask-Migrate replaces that. I guess you can inspect the sqlite table using the sqlite3 client, but if running manage.py db upgrade several times tells you that you are already at the latest revision, I think the database file is going to be all right.

Can't really think of a reason why the server will not find the tables. You may want to try loading and saving users from a shell session as shown in the book.

Also, another question. Do you get a successful unit test run?

@JWTappert
Copy link
Author

Yes, all my unit tests up until chapter 8 have passed.

So when loading and saving users through the shell, I get this error: OperationalError: (sqlite3.OperationalError) no such table: users [SQL: u'INSERT INTO users (email, username, password_hash, role_id) VALUES (?, ?, ?, ?)'] [parameters: ('tappertj@oregonstate.edu', 'tappertj', 'pbkdf2:sha1:1000$4YdTaCvj$c5bf1bd3ccefe1d373e26027ddcb9c26b36d7f51', None)]

but when I then do: print u.username it returns tappertj

@miguelgrinberg
Copy link
Owner

Okay. That is consistent with the theory that the database is empty. The object is fine, but loading or saving gives you a "no such table" error.

You didn't answer a question I asked above. Do you have any changes from the code in this repository? If you do, add the output of git diff here.

@JWTappert
Copy link
Author

I unfortunately didn't pull the repo but started from scratch and have been following along.

@miguelgrinberg
Copy link
Owner

Can I see your config.py file, and also, if you have any environment variables that you set for this application to run, the values of those as well.

@JWTappert
Copy link
Author

I did not set any environment variables.

import os
basedir = os.path.abspath(os.path.dirname(__file__))


class Config:
	SECRET_KEY = os.environ.get('SECRET_KEY') or 'hard to guess string' 
	SQLALCHEMY_COMMIT_ON_TEARDOWN = True
	CTC_MAIL_SUBJECT_PREFIX = '[Cascades Tech Club]'
	CTC_MAIL_SENDER = 'CTC Admin <flasky@example.com>' 
	CTC_ADMIN = os.environ.get('CTC_ADMIN')
	WTF_CSRF_ENABLED = True

	@staticmethod
	def init_app(app): 
		pass


class DevelopmentConfig(Config):
	DEBUG = True
	MAIL_SERVER = 'smtp.googlemail.com'
	MAIL_PORT = 587
	MAIL_USE_TLS = True
	MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
	MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD') 
	SQLALCHEMY_DATABASE_URI = os.environ.get('DEV_DATABASE_URL') or \
        'sqlite:///' + os.path.join(basedir, 'data-dev.sqlite')

class TestingConfig(Config): 
	TESTING = True
	SQLALCHEMY_DATABASE_URI = os.environ.get('TEST_DATABASE_URL') or \
		'sqlite:///' + os.path.join(basedir, 'data-test.sqlite')

class ProductionConfig(Config):
	SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
    	'sqlite:///' + os.path.join(basedir, 'data.sqlite')

config = {
    'development': DevelopmentConfig,
    'testing': TestingConfig,
    'production': ProductionConfig,
    'default': DevelopmentConfig
}

@miguelgrinberg
Copy link
Owner

The config looks okay. What's the output of python manage.py db history?

@JWTappert
Copy link
Author

/usr/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py:800: UserWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True to suppress this warning.
  warnings.warn('SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True to suppress this warning.')

@miguelgrinberg
Copy link
Owner

Only those two lines? What files do you have in the migrations/versions directory?

@JWTappert
Copy link
Author

Yeah. the versions directory is empty as well.

@miguelgrinberg
Copy link
Owner

Okay, we are finally getting somewhere. The problem is that you did not generate a migration for your database. This is the manage.py db migrate command, have you done that?

@JWTappert
Copy link
Author

I thought I did. I have a migrations directory and everything.

@JWTappert
Copy link
Author

Oh I see. I ran that command and now I have a new version file.

So how often should I run that command?

@miguelgrinberg
Copy link
Owner

You need to generate a migration any time you make a change to your database. After you generate the migration you have to run the upgrade command to apply the changes to the database.

I recommend that you reread the Flask-Migrate section of chapter 5 to have a full understanding of things. It isn't always as simple as generating the migration and then running the upgrade, sometimes you need to do more than that.

@JWTappert
Copy link
Author

Understood. Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants