我决定用烧瓶、postgresql和传单来编写一个小型的and应用程序。我想使用postgresql的PostGIS扩展程序来存储坐标(纬度和经度)。我的烧瓶应用程序使用了,蓝图,尤其是用于数据库迁移过程的烧瓶-迁移。
以下是我的数据库模型的摘录:
from . import db
from geoalchemy2 import Geometry
class Station(db.Model):
__tablename__ = 'stations'
id = db.Column(db.Integer, primary_key=True, unique=True)
name = db.Column(db.String(255))
position = db.Column(Geometry('Point', srid=4326))
这里摘录了我的应用程序/init.py
import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from config import config
db = SQLAlchemy()
migrate = Migrate()
def create_app(config_name=None, main=True):
if config_name is None:
config_name = os.environ.get('FLASK_CONFIG', 'development')
app = Flask(__name__)
app.config.from_object(config[config_name])
db.init_app(app)
migrate.init_app(app, db)
from .home import home as home_blueprint
app.register_blueprint(home_blueprint)
from .admin import admin as admin_blueprint
app.register_blueprint(admin_blueprint, url_prefix='/admin')
return app
在尝试使用特定的扩展程序之前,我没有任何问题来调整我的模型。从那时起,迁移进行得很顺利,但是升级不起作用(python manage.py db升级)。
在这里,我从Here服务器获得的日志:
sa.Column('position', geoalchemy2.types.Geometry(geometry_type='POINT', srid=4326), nullable=True),
`enter code here`NameError: name 'geoalchemy2' is not defined
我有点迷茫,在这个具体问题上找不到多少帮助。知道是什么导致了这个问题吗?
发布于 2016-08-29 21:14:03
Alembic不尝试确定和呈现迁移脚本中自定义类型的所有导入。编辑生成的脚本以包含from geoalchemy2.types import Geometry
,并将列def更改为只使用Geometry
。
在运行之前,您应该始终检查自动生成的脚本。脚本中甚至有这样的评论。
发布于 2022-01-04 03:34:06
另一种不需要手工编辑修改的方法是将import geoalchemy2
添加到alembic/script.py.mako
中,然后每次都添加模块。
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from alembic import op
import sqlalchemy as sa
import geoalchemy2 # <--- right here
${imports if imports else ""}
# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}
def upgrade():
${upgrades if upgrades else "pass"}
def downgrade():
${downgrades if downgrades else "pass"}
https://stackoverflow.com/questions/39215278
复制相似问题