omab / python-social-auth

Social auth made simple
http://psa.matiasaguirre.net
BSD 3-Clause "New" or "Revised" License
2.83k stars 1.09k forks source link

Database migrations for Flask app #1055

Closed egorov-aleksey closed 8 years ago

egorov-aleksey commented 8 years ago

How get database migrations for Flask app? Because I got error: sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) relation "social_auth_usersocialauth" does not exist.

For Django app it is http://psa.matiasaguirre.net/docs/configuration/django.html#database

My Flask app manage.py:

from app.models import *
from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager

migrate = Migrate(app, db)

manager = Manager(app)
manager.add_command('db', MigrateCommand)

if __name__ == '__main__':
manager.run()

What I should do for generate database migrations for Flask app?

alexpantyukhin commented 8 years ago

Hi @meatbot. There is an example of flask app for flask and sqlalchemy https://github.com/omab/python-social-auth/tree/master/examples/flask_example . Please, look on manage.py file (https://github.com/omab/python-social-auth/blob/master/examples/flask_example/manage.py) you can find there a method syncdb. If you run it '.manage.py syncdb' this command create all needed tables for you. Hopefully this advise will be useful.

egorov-aleksey commented 8 years ago

@alexpantyukhin, I tried to use it. But i have a problem.

I add syncdb command to my manage.py:

from app.models import *
from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager

migrate = Migrate(app, db)

manager = Manager(app)
manager.add_command('db', MigrateCommand)

@manager.command
def syncdb():
    from social.apps.flask_app.default import models
    models.PSABase.metadata.create_all(db.engine)

if __name__ == '__main__':
    manager.run()

And python manage.py syncdb successfully create relations social_auth_association, social_auth_code, social_auth_nonce, social_auth_usersocialauth. It is great!

But when i run python manage.py db migrate I got a migration which removes social_auth_* relations:

...
def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('social_auth_nonce')
    op.drop_table('social_auth_usersocialauth')
    op.drop_table('social_auth_code')
    op.drop_table('social_auth_association')
    ### end Alembic commands ###
...

What I should do? What am I doing wrong?

alexpantyukhin commented 8 years ago

I guess I know what the problem. It seems you use flask_migrate and social_auth tables is not in the same session. That's why migration thinks that these tables are redundant. Did you made a call of init_social?

egorov-aleksey commented 8 years ago

I call init_social in my models.py module:

from app import app
from flask_sqlalchemy import SQLAlchemy
...
db = SQLAlchemy(app)
...
class User(db.Model):
...
init_social(app, db.session)

You about this?

egorov-aleksey commented 8 years ago

I found perfect solution http://stackoverflow.com/a/35196770/2111562