R
R
RaymanPy2020-06-07 11:02:24
Flask
RaymanPy, 2020-06-07 11:02:24

Flask-Migrate not seeing db changes?

Let's say I change the price parameter from db.Float to db.Integer
After that I want to migrate the database
But as you can see, alembic does not see the changes in the
database: Heroku Postgres

flask db migrate
INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
INFO  [alembic.ddl.postgresql] Detected sequence named 'phone_id_seq' as owned by integer column 'phone(id)', assuming SERIAL and omitting
INFO  [alembic.env] No changes in schema detected.


__init__.py
from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate

app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)

migrate = Migrate(app,db)


from app import routes,models


models.py
class Phone(db.Model):

  id = db.Column(db.Integer,primary_key=True)
  name = db.Column(db.String)
  value = db.Column(db.Float,default=0)
  price = db.Column(db.Float,default=0)
  percent = db.Column(db.Integer,default=0)
  weight = db.Column(db.Float,default=0)
  tarif = db.Column(db.Float,default=0)
  other = db.Column(db.Float,default=0)
  course_dollar = db.Column(db.Float,default=0)

  def __init__(self,name):
    self.name = name


  def __repr__(self):
    return f'<name {self.name}>'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
domanskiy, 2020-09-04
@domanskiy

Try
stamp head
migrate
upgrade
A more radical option.
The docs state that compare_type is False by default. Because of this, Auto Generating Migrations does not "see" changes in column types.
How I won it.
In the migrations folder in the env.py file, we change the def run_migrations_online () function:
Option 1
compare_type = True

with connectable.connect() as connection:
        context.configure(
            connection=connection,
            target_metadata=target_metadata,
            process_revision_directives=process_revision_directives,
            **current_app.extensions['migrate'].configure_args,
            # compare_type=True
            compare_type=my_compare_type

Option 2
Add the my_compare_type function to the parameter in run_migrations_online
def my_compare_type(context, inspected_column,
                        metadata_column, inspected_type, metadata_type):
        # return False if the metadata_type is the same as the inspected_type
        # or None to allow the default implementation to compare these
        # types. a return value of True means the two types do not
        # match and should result in a type change operation.
        return None


    with connectable.connect() as connection:
        context.configure(
            connection=connection,
            target_metadata=target_metadata,
            process_revision_directives=process_revision_directives,
            **current_app.extensions['migrate'].configure_args,
            compare_type=my_compare_type

        )

Next
, manage.py db migrate -m "Initial migration."
Then manage.py db upgrade

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question