miguelgrinberg / Flask-Migrate

SQLAlchemy database migrations for Flask applications using Alembic
MIT License
2.35k stars 227 forks source link

After deleting migrations file some error shows please edit the configuration in alembic.ini file #357

Closed ganeshmastud closed 4 years ago

ganeshmastud commented 4 years ago

Why I deleted the migrations file? is because whenever I tried to run migrate command it dops my table to overcome this issue I found some answer on StackOverflow then I added

from app import db
from datetime import datetime
from werkzeug.security import generate_password_hash,check_password_hash

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), index=True, unique=True)
    email = db.Column(db.String(120), index=True, unique=True)
    password_hash = db.Column(db.String(128))
    fooditems = db.relationship('Fooditem',cascade="all,delete",backref=db.backref('user', lazy='joined'), lazy='dynamic')
    #db.relationship('Line', cascade="all,delete", backref=db.backref('song', lazy='joined'), lazy='dynamic')
    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.password_hash, password)
    def __repr__(self):
        return '<User {}>'.format(self.username)

class Fooditem(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(140), nullable=False)
    calories = db.Column(db.Float, default=0.0)
    fat = db.Column(db.Float, default=0.0)
    protein = db.Column(db.Float, default=0.0)
    carbs = db.Column(db.Float, default=0.0)
    sugar = db.Column(db.Float, default=0.0)
    fiber = db.Column(db.Float, default=0.0)
    calcium = db.Column(db.Float,default=0.0, nullable=False)
    date = db.Column(db.DateTime, index=True, default=datetime.utcnow)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

def init():
    db.create_all()

if __name__=='__main__':
    init()

in my models file

################################################################# alembic file

# A generic, single database configuration.

[alembic]
# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s

# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false

# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = WARN
handlers = console
qualname =

[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine

[logger_alembic]
level = INFO
handlers =
qualname = alembic

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S

######################################################################### when tried to perform migrate

(venv) C:\Users\mastud\PycharmProjects\FoodTracker>flask db migrate
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
ERROR [root] Error: Can't locate revision identified by '045303980ab9'

######################################################################### config file

import os

basedir = os.path.abspath(os.path.dirname(__file__))
class Config(object):
    SECRET_KEY = os.environ.get('you-will-never-guess-SECRET_KEY') or 'you-will-never-guess'
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
                              'sqlite:///' + os.path.join(basedir, 'app.db')
    SQLALCHEMY_TRACK_MODIFICATIONS = False

###########################################################################

ganeshmastud commented 4 years ago

Please edit configuration/connection/logging settings in 'C:\Users\mastud\PycharmProjects\FoodTracker\migrations\alembic.ini' before proceeding. this is issue

miguelgrinberg commented 4 years ago

Sorry, but I don't understand. You have a call to create_all() in your application. Why is that? That competes with Flask-Migrate. If you want to manage your database with Flask-Migrate then you can't create your tables by directly going to SQLAlchemy.

Also you seem to have deleted a migration file that was in use, so after that you are not going to have a valid migration repository. I'm not sure how to help you, it is unclear to me what you need to do.

ganeshmastud commented 4 years ago

an error has been solved by making creating a new virtual environment and adding these file in that venv of a new project, after 2day search I found the answer in StackOverflow that only one thing that I required is to add one line in env.py file in migrations folder is from models import User, Fooditem sir if you know what it means can you pls explain me

miguelgrinberg commented 4 years ago

@ganeshmastud You appear to be copying things from stack overflow without understanding them. On the other side, I mentioned a couple of concrete problems with your code and you are ignoring it. I'm sorry but I can't really help. If you found a solution to your problem then great, if not, then you need to make an effort and explain the problem better, and when I ask you a question answer it.