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

Can't create db #18

Closed
karl-kallavus opened this issue Jul 11, 2014 · 21 comments
Closed

Can't create db #18

karl-kallavus opened this issue Jul 11, 2014 · 21 comments
Labels

Comments

@karl-kallavus
Copy link

Dear Miguel,

thanks you for your book,

I have some troubles, I am on chapter 8a.

I can't create a db, db init, db downgrade etc - it's ok,
but when I start a server and add some text to form - I have this error:

sqlalchemy.exc.OperationalError

OperationalError: (OperationalError) no such table: users u'SELECT users.id AS users_id, users.username AS users_username, users.role_id AS users_role_id, users.password_hash AS users_password_hash \nFROM users \nWHERE users.username = ?\n LIMIT ? OFFSET ?' (u'dfh', 1, 0)
@karl-kallavus
Copy link
Author

I use db.create_all() via python manage.py shell
Problem solved.

@miguelgrinberg
Copy link
Owner

Using db.create_all() is not the correct solution, when you do that you are bypassing the database migration support that is built into the application.

The equivalent to db.create_all() when you use Flask-Migrate is this command:

$ python manage.py db upgrade

Did you try that?

@karl-kallavus
Copy link
Author

Yes, I tried that:

$ git clone https://github.com/miguelgrinberg/flasky.git
$ cd flasky
$ git checkout 8a

$ python manage.py db init
$ python manage.py db upgrade
$ python manage.py runserver

After that I tried to add some text to form and submit but I have this error message:

sqlalchemy.exc.OperationalError

OperationalError: (OperationalError) no such table: users u'SELECT users.id AS users_id, users.username AS users_username, users.role_id AS users_role_id, users.password_hash AS users_password_hash \nFROM users \nWHERE users.username = ?\n LIMIT ? OFFSET ?' (u'xdfh', 1, 0)

@miguelgrinberg
Copy link
Owner

The db init command is used to initialize a new repository. You do this on your own project at the very beginning when you want to begin using migrations. For flasky this was obviously done by me when I created the project, so when you do it again you erase the entire migration history.

The correct sequence is:

$ git clone https://github.com/miguelgrinberg/flasky.git
$ cd flasky
$ git checkout 8a

$ python manage.py db upgrade
$ python manage.py runserver

Let me know if this does it.

@karl-kallavus
Copy link
Author

This sequnce doesn't work, I tried.

After db.create_all() via manage.py shell - application work correct.

@oztalha
Copy link

oztalha commented Jul 11, 2014

@karl-kallavus I didn't checkout 8a particularly but manage.py db init works for me. You might try removing the migrations folder first, also db.drop_all() if you've already created the db via db.create_all() before.
@miguelgrinberg are you in Portland, good morning, wow you're responding at 7am :-)

@miguelgrinberg
Copy link
Owner

Guys, no, stop.

Forget about db.create_all(), you do not want to use that, as it competes with migrations. If you use that then you won't have migrations, so don't.

The sequence I gave you works. Do not issue db init, that is for new projects. You just need to issue db upgrade. If you delete the database file and then do db upgrade all the migrations will run and you will have a good database (identical to what you get with create_all(), but without disabling migrations).

Give that a try, and if that doesn't work, please paste the output of your terminal.

PS: I'm in Texas right now, so it was 9am when I replied. Unfortunately my brain & body are still in West Coast time, so it felt like 7am to me ;-)

@oztalha
Copy link

oztalha commented Jul 12, 2014

I just mentioned db.drop_all() to let him a start fresh. I'm using db upgrade as suggested. Flask-Migrate works perfect. Thanks !

@kodemi
Copy link

kodemi commented Oct 8, 2014

git checkout 8a
rm data-dev.sqlite
python manage.py db upgrade
python manage.py runserver
OperationalError: (OperationalError) no such column: users.password_hash u'SELECT users.id AS users_id, users.username AS users_username, users.role_id AS users_role_id, users.password_hash AS users_password_hash \nFROM users \nWHERE users.username = ?\n LIMIT ? OFFSET ?' (u'Denis', 1, 0)

In flasky/migrations/versions I can see only initial migration, but you added new column to db (here and in next sections of chapter 8).
Solution for me:

python manage.py db migrate
python manage.py db upgrade

@kodemi
Copy link

kodemi commented Oct 8, 2014

Sorry. I hurried. 8c checkout has all needed migrations.

@MrReN
Copy link

MrReN commented Feb 18, 2015

@kodemi ,thanks

@impactanalysts
Copy link

I ran into the same error posted by karl-kallavus. Miguel's solution (upgrade db, then run server) worked for me. Thanks Miguel!

@Quirel
Copy link

Quirel commented Sep 11, 2015

I have same problem as @karl-kallavus. (I used ubuntu and python 3.4, also windows 7 and python 3.4)
Tried:
1)
rm data-dev.sqlite
python manage.py db upgrade
It doesn't help.

Then deactivated venv.
Created another directory and done:
$ git clone https://github.com/miguelgrinberg/flasky.git
$ cd flasky
$ git checkout 8a

created new venv, activated, installed all from requirements.txt

$ python manage.py db upgrade
$ python manage.py runserver

And result is the same. I tried it on windows and linux (on different machines)

Also tried deleting migrations/ and *.sqlite.
Then
$ python manage.py db init
$ python manage.py db migrate (And there were users table and roles table in logs)
$ python manage.py db upgrade
... The result was same. Same error about 'no such table...'

Update

Tried checkout 8c.
Now it works. So, I'll try to read further from 8a to 8c and try to find problem.

@miguelgrinberg
Copy link
Owner

@Quirel just to make sure other people seeing this don't get confused, this line:

python manage.py shell db upgrade

should be changed to:

python manage.py db upgrade

@Quirel
Copy link

Quirel commented Sep 12, 2015

@miguelgrinberg fixed, thank you. (It was about 4:00 am, when I wrote comment)

update

I've found my mistake.
In app/init.py:
I had:
db = SQLAlchemy(app)
It should be:
db = SQLAlchemy()
Because we changed our initialization method via creating function create_app()

P.S. @miguelgrinberg, I think that when I've cloned your repo and had the same mistake, I just started server from my repo (forget to change dir or smth else.. as I said it was about 4 am.)

@xinqiu
Copy link

xinqiu commented Dec 3, 2015

Thx, and it helps me very much. I was confused about this for several hours.

@Morriaty-The-Murderer
Copy link

Hello, @miguelgrinberg
While creating db on Heroku,why it failed?

File "migrations/env.py", line 87, in <module>
    run_migrations_online()
  File "migrations/env.py", line 76, in run_migrations_online
    **current_app.extensions['migrate'].configure_args)
AttributeError: '_MigrateConfig' object has no attribute 'configure_args'

@miguelgrinberg
Copy link
Owner

@Morriaty-The-Murderer My guess is that the version of Flask-Migrate you have in your requirements.txt file does not match the version that is in your local virtualenv. Can you check?

@Morriaty-The-Murderer
Copy link

@miguelgrinberg
I specify the version, and it worked, thank you very much!

@zuizao
Copy link

zuizao commented Jun 11, 2016

python manage.py db migrate
python manage.py db upgrade
this works for me, thanks @kodemi

@spaceled
Copy link

I have a such Error. I tried to upgrade my DB by your instructions. But I does not upgrade my db its just run like as

$ python manage.py runserver

Please, check the image I attached here.

http://tinypic.com/r/2eqco4w/9

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