我正在使用Alembic来处理Flask的迁移.alembic revision --autogenerate理论上,应根据数据库中的更改自动生成迁移.但是,Alembic只是使用上述命令生成空白迁移.
有一个问题与这个问题非常相似,问题在于没有导入正确的模型.但是,我从Flask应用程序导入了模型,如下所示env.py:
...
# import settings from Flask
alembic_config = config.get_section(config.config_ini_section)
from start import app
from models import User, Item, Recipient # models are imported here from models.py
alembic_config['sqlalchemy.url'] = app.config['SQLALCHEMY_DATABASE_URI']
engine = engine_from_config(
alembic_config, # config.get_section(config.config_ini_section)
prefix='sqlalchemy.',
poolclass=pool.NullPool)
...
Run Code Online (Sandbox Code Playgroud)
以及导入的db元数据env.py('start'是我的Flask应用程序主文件的名称):
...
from start import db
target_metadata = db.metadata
...
Run Code Online (Sandbox Code Playgroud)
alembic revision --autogenerate -m "initial_rev"然后运行生成一个空迁移,虽然我的Flask应用程序会有所不同:
"""initial_rev
Revision ID: 45296fd29540
Revises: None
Create Date: 2013-06-19 17:32:38.392268
"""
# revision identifiers, …Run Code Online (Sandbox Code Playgroud) 我正在为朋友zumba类开发一个小型注册应用程序,使用Flask,SQLAlchemy和Flask-migrate(alembic)来处理db update.我决定使用SQlite,因为应用程序必须是自包含的并且在没有Internet访问的笔记本电脑上本地运行,并且SQLite不需要安装服务或其他服务,这也是必须的.
处理SQLite缺乏对ALTER表的支持在初始开发期间不是问题,因为我只是销毁,在出现问题时重新创建数据库.但是现在我的朋友实际上正在使用该应用程序我遇到了问题.
在功能请求之后,必须修改一个表,并再一次得到可怕的""不支持SQLite方言中约束的ALTER".我预见这个问题将来也可能出现.
我该如何处理这个问题?在处理数据库方面,我几乎是一个新手.我读到一种处理方法是创建一个新表,创建新约束并复制数据并重命名表,但我不知道如何在alembic脚本中实现它.
我正在使用Alembic进行Flask项目中的迁移实现.有一个alembic.ini文件必须指定数据库配置:
sqlalchemy.url = driver://user:password@host/dbname
有没有办法从环境变量中指定参数?我试图以这种方式加载它们$(env_var)但没有成功.谢谢!
我在一个团队中使用alembic来管理数据库迁移.我最近拉了主人,并试图跑alembic upgrade heads.我收到以下消息;
INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO [alembic.runtime.migration] Will assume transactional DDL.
ERROR [alembic.util.messaging] Requested revision a04c53fd8c74 overlaps with other requested revisions 453d88f67d34
FAILED: Requested revision a04c53fd8c74 overlaps with other requested revisions 453d88f67d34
Run Code Online (Sandbox Code Playgroud)
我试图跑的时候得到了同样的信息alembic downgrade -1.跑步alembic history打印这个;
453d88f67d34 -> a04c53fd8c74 (label_1, label_2) (head), Create such and such tables.
2f15c778e709, 9NZSZX -> 453d88f67d34 (label_1, label_2) (mergepoint), empty message
b1861bb8b23f, b8aa3acdf260 -> 2f15c778e709 (label_1, label_2) (mergepoint), Merge heads b18 and b8a
(...many more old …Run Code Online (Sandbox Code Playgroud) 假设我的db模型包含一个对象User:
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(String(32), primary_key=True, default=...)
name = Column(Unicode(100))
Run Code Online (Sandbox Code Playgroud)
我的数据库包含一个包含n行的users表.在某些时候,我决定拆分成和,而在我想我的数据迁移以及.namefirstnamelastnamealembic upgrade head
自动生成的Alembic迁移如下:
def upgrade():
op.add_column('users', sa.Column('lastname', sa.Unicode(length=50), nullable=True))
op.add_column('users', sa.Column('firstname', sa.Unicode(length=50), nullable=True))
# Assuming that the two new columns have been committed and exist at
# this point, I would like to iterate over all rows of the name column,
# split the string, write it into the new …Run Code Online (Sandbox Code Playgroud) 我有以下项目结构
--some db:
--some db:
--alchemy:
-- __init__.py
--alembic:
-- versions
-- env.py
-- README.py
-- script.py
--migrations:
-- __init__.py
--models:
-- model_1
-- model_2
-- __init__.py
Run Code Online (Sandbox Code Playgroud)
我尝试通过 alembic 自动生成迁移。
我Base在__init__.py模型中的文件夹
import sqlalchemy as sa
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base, declared_attr
metadata = sa.MetaData()
Base = declarative_base(metadata=metadata)
Run Code Online (Sandbox Code Playgroud)
并导入这是 env.py
from logging.config import fileConfig
from alembic import context
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from models import Base
config = context.config
fileConfig(config.config_file_name) …Run Code Online (Sandbox Code Playgroud) 我目前有一个包含HTML标记的列.在该标记内,有一个我想要存储在新列中的时间戳(因此我可以查询它).我的想法是在一次迁移中执行以下操作:
但是当我尝试运行迁移时,它似乎陷入无限循环.这是我到目前为止所得到的:
def _extract_publication_date(html):
root = html5lib.parse(html, treebuilder='lxml', namespaceHTMLElements=False)
publication_date_string = root.xpath("//a/@data-datetime")[0]
return parse_date(publication_date)
def _update_tip(tip):
tip.publication_date = _extract_publication_date(tip.rendered_html)
tip.save()
def upgrade():
op.add_column('tip', sa.Column('publication_date', sa.DateTime(timezone=True)))
tips = Tip.query.all()
map(tips, _update_tip)
def downgrade():
op.drop_column('tip', 'publication_date')
Run Code Online (Sandbox Code Playgroud) 我想使用Alembic将数据库的列类型从字符串更改为整数.如果我使用纯SQL,它实现了目标:
alter table statistic_ticket alter column tags type bigint using tags::bigint;
Run Code Online (Sandbox Code Playgroud)
但是当我使用Alembic时:
import sqlalchemy as sa
def upgrade():
op.alter_column('statistic_ticket', 'tags', nullable = True, existing_type=sa.String(length=255), type_=sa.Integer, existing_nullable=True)
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
HINT: Please use USING clause for carrying out the conversion
Run Code Online (Sandbox Code Playgroud)
SQLAlchemy生成的SQL语句是:
ALTER TABLE statistic_ticket ALTER COLUMN tags TYPE INTEGER' {}
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我怎么做alembic或SQLAlchemy中的SQL op.execute(SQL)吗?
我试图改变Alembic但是当我尝试运行Alembic时我得到了错误.我是alembic的新手,请告诉我为什么我会收到此错误,我该如何解决?
我可以alembic.ini在迁移文件夹和Alembic使用的修订标识符中看到,一切似乎都很好.
$alembic current
No handlers could be found for logger "alembic.util"
FAILED: No config file 'alembic.ini' found, or file has no '[alembic]' section
Run Code Online (Sandbox Code Playgroud)
20c921506336_.py:
"""empty message
Revision ID: 20c921506336
Revises: None
Create Date: 2015-01-30 16:28:38.981023
"""
# revision identifiers, used by Alembic.
revision = '20c921506336'
down_revision = None
from alembic import op
import sqlalchemy as sa
def upgrade():
### commands auto generated by Alembic - please adjust! ###
op.create_table('user',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=50), nullable=True),
sa.Column('email', …Run Code Online (Sandbox Code Playgroud) alembic ×10
python ×7
sqlalchemy ×5
flask ×2
fastapi ×1
ini ×1
logging ×1
postgresql ×1
python-3.x ×1
sqlite ×1
uvicorn ×1