- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个数据库x
,每个表中都填充了一些数据。我想创建该数据库的副本(具有相同的架构和精确的数据)。首先,我使用 automap_base 创建 x
的声明性基类。
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session as s
def name_for_scalar_relationship(base, local_cls, referred_cls, constraint):
name = referred_cls.__name__.lower() + "_ref"
return name
Base = automap_base()
# engine, refering to the original database
engine = create_engine("mysql+pymysql://root:password1@localhost:3306/x")
# reflect the tables
Base.prepare(engine, reflect=True, name_for_scalar_relationship=name_for_scalar_relationship)
Router = Base.classes.router
########check the data in Router table
session = s(engine)
r1 = session.query(Router).all()
for n in r1:
print(n.name) #This returns all the router names
从here获得一些帮助我使用alembic
升级位于不同位置mysql+pymysql://anum:Anum-6630@localhost:3306/y
的数据库y
.
from sqlalchemy.orm import sessionmaker as sm
from sqlalchemy import create_engine
from alembic import op
# revision identifiers, used by Alembic.
revision = 'fae98f65a6ff'
down_revision = None
branch_labels = None
depends_on = None
def upgrade():
bind = op.get_bind()
session = sm(bind=bind)
Base.metadata.create_all(bind=bind)
# session._add_bind(session, bind=bind)
session.add(Router(id=uuid.uuid().bytes, serial="Test1"))
session.commit()
Base.metadata.create_all(bind=bind)
行实际上将所有表(包括适当的FK约束)添加到数据库y
中,但所有表都是空的,除了我手动添加的路由器表中的一项。我尝试使用 create_all() 但效果不佳。有没有办法将所有数据从x
复制到y
数据库?
最佳答案
由于没有人回答,这是我进行复制的狂野方法:因为需要按顺序创建表(以避免 FK 约束错误),所以我必须定义一个包含每个表的有序列表
缓慢且不可靠的解决方案:
allTables = ["tableA",
"tableB", # <table B points to FK constraint of tableA>
"tableC", # <table C points to FK constraint of tableB>
...]
def copyAllContent():
global allTables
s = Session(bind=origEngine) # session bind to original table
se = Session(bind=op.get_bind()) # session bind to cloned table (currently empty)
try:
for table in allTables:
# print(table)
rows = s.query(Base.classes._data[table]).all()
for row in rows:
local_object = se.merge(row) #merging both sessions
se.add(local_object)
se.commit()
except Exception as e:
print(e)
上述方法适用于大多数表,但并非全部。例如表router
存在于原始数据库中,但我仍然在s.query(Base.classes._data[table]).all()
中收到错误,名称不存在键路由器
。没有足够的时间来深入研究解决方案。
快速可靠的解决方案:
后来我发现from here另一个使用 mysqldump
#copy sql dump from x database
mysqldump --column-statistics=0 -P 8000 -h localhost -u root -p --hex-blob x > x_dump.sql
上面的命令行mysqldump
命令创建一个名为x_dump.sql
的sql转储文件,其中包含重新生成数据库所需的所有必要的SQL脚本。现在我们需要做的就是将此 sql 转储文件应用到另一个数据库 y
#clone the database contents into y database
mysql -P 3306 -h localhost -u anum -p y < x_dump.sql
这是执行相同操作的Pythonic版本
import subprocess
#copy sql dump from x database - blocking call (use Popen for non-blocking)
print(subprocess.call(["mysqldump", "--column-statistics=0", '-P', '8000', '-h', 'localhost', '-u', '<user>', '-p<password>',
'--hex-blob', 'x', '>', 'x_dump.sql'], shell=True))
print("done taking dump.")
#clone the database contents into y database - blocking call
print(subprocess.call(["mysql", '-P', '3306', '-h', 'localhost', '-u', '<user>', '-p<password>',
'y', '<', 'x_dump.sql'], shell=True))
print("done cloning the sqlDump.")
关于python - 使用 automap_base 和 alembic 迁移/复制数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53434215/
我正在尝试将我的 Flask 项目与 Alembic 我的应用程序结构看起来像 project/ configuration/ __init__.
我将 Alembic 与 SQLAlchemy 结合使用来执行模式迁移 和 data migration . 由于我在项目的主应用程序 myapp 中将特定于迁移的依赖项存储在它们自己的模块中(根据
这是已经发生和正在发生的事件链 第 0 天:我开发并部署了我的应用程序 第 1 天:我创建了新数据库 第 3 天:我意识到我想在现有表中添加一个新行。我找到了flask-migrate,我想用它来迁移
我正在尝试在 Alembic 上创建一个新的迁移,它将新枚举类型的新列添加到现有表中。但是我收到一个我认为 Alembic 会自动处理的错误。 我使用的是 Postgres 9.6.6、Alembic
我将 Alembic 与 SQLAlchemy 一起使用。使用 SQLAlchemy,我倾向于遵循不将连接字符串与版本化代码一起存储的模式。相反,我有包含任何 secret 信息的文件 secret.
背景 我正在尝试在此 Flask + RESTplus server example 中使用 PostgreSQL 后端而不是 Sqlite . 我遇到了 PasswordType 数据库列类型的问题
我在一个使用 alembic 管理数据库迁移的团队中工作。我最近拉了master,并尝试运行alembic upgrade heads .我收到以下消息; INFO [alembic.runtime
我想为 Flask 应用程序进行迁移。我正在使用 Alembic。 但是,我收到以下错误。 Target database is not up to date. 在线,我读到这与此有关。 http:/
我正在从事一个使用 SQLite 作为数据库并使用 Alembic 作为数据库迁移工具的项目。它包含空间数据,因此空间扩展和 geoalchemy2 包含在项目中。我正在使用 autogenerate
这个问题在这里已经有了答案: change column datatype from array to integer (2 个答案) 关闭 3 年前。 我有一个这样的模型: class Sched
当我想使用 alembic 进行自动生成迁移时出现错误。 项目树: - alembic.ini - axis.py - tree.txt - alembic - en
我正在使用 Alembic 来管理数据库的迁移。多个 Python 包使用同一个数据库,每个包都有自己的迁移路径。 在生成自动迁移时,如何告诉 Alembic 忽略其他包中的表?例如,当我运行时:
我有一个看起来像这样的表 > select * from mytable id value 0 1 hello world 1 2 hello_world 2 3 hel
我正在使用 alembic 根据用户定义的 sqlalchemy 模型管理数据库迁移。我的挑战是 我希望 alembic 忽略对特定表集的任何创建、删除或更改。 注:我的 Q 与这个问题类似 Igno
我有一个 SQLAlchemy 模型,例如 - class UserFavPlace(db.Model): # This model stores the feedback from the
我需要通过向现有表添加一张表和一列来更新我的数据库。 新列和表应该是一对多的关系。 这是 Alembic 修订文件: def upgrade(): op.create_table('categ
我有一个表 'test' 有一个没有约束的列 'Name'。我需要ALTER给它一个 UNIQUE约束。我该怎么做? 我应该使用 op.alter_column('???')或 create_uniq
我有下表 mysql> describe table; +----------------+-------------+------+-----+-------------------+-------
我最初将我的一个 SQLAlchemy 模型定义为: class StreetSegment(db.Model): id = db.Column(db.Integer, autoincreme
我正在使用 Flask-Migrate==2.0.0。它没有正确检测到变化。每次我运行 python manage db migrate 它都会为所有模型生成一个脚本,尽管它们已在以前的修订版中成功添
我是一名优秀的程序员,十分优秀!