👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
I need to alter data during an Alembic upgrade.
I currently have a "players" table in a first revision:
def upgrade():
op.create_table("player",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("name", sa.Unicode(length=200), nullable=False),
sa.Column("position", sa.Unicode(length=200), nullable=True),
sa.Column("team", sa.Unicode(length=100), nullable=True)
sa.PrimaryKeyConstraint("id")
)
I want to introduce a "teams" table. I"ve created a second revision:
def upgrade():
op.create_table("teams",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("name", sa.String(length=80), nullable=False)
)
op.add_column("players", sa.Column("team_id", sa.Integer(), nullable=False))
I would like the second migration to also add the following data:
Populate teams table:
INSERT INTO teams (name) SELECT DISTINCT team FROM players;
Update players.team_id based on players.team name:
UPDATE players AS p JOIN teams AS t SET p.team_id = t.id WHERE p.team = t.name;
How do I execute inserts and updates inside the upgrade script?
👻 Read also: what is the best laptop for engineering students?
We hope this article has helped you to resolve the problem. Apart from How do I execute inserts and updates in an Alembic upgrade script?, check other code Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano How do I execute inserts and updates in an Alembic upgrade script?
- Deutsch How do I execute inserts and updates in an Alembic upgrade script?
- Français How do I execute inserts and updates in an Alembic upgrade script?
- Español How do I execute inserts and updates in an Alembic upgrade script?
- Türk How do I execute inserts and updates in an Alembic upgrade script?
- Русский How do I execute inserts and updates in an Alembic upgrade script?
- Português How do I execute inserts and updates in an Alembic upgrade script?
- Polski How do I execute inserts and updates in an Alembic upgrade script?
- Nederlandse How do I execute inserts and updates in an Alembic upgrade script?
- 中文 How do I execute inserts and updates in an Alembic upgrade script?
- 한국어 How do I execute inserts and updates in an Alembic upgrade script?
- 日本語 How do I execute inserts and updates in an Alembic upgrade script?
- हिन्दी How do I execute inserts and updates in an Alembic upgrade script?
New York | 2023-03-22
Simply put and clear. Thank you for sharing. How do I execute inserts and updates in an Alembic upgrade script? and other issues with StackOverflow was always my weak point 😁. I just hope that will not emerge anymore
Tallinn | 2023-03-22
Simply put and clear. Thank you for sharing. How do I execute inserts and updates in an Alembic upgrade script? and other issues with Python-Funktionen und -Methoden was always my weak point 😁. Will use it in my bachelor thesis
Abu Dhabi | 2023-03-22
Thanks for explaining! I was stuck with How do I execute inserts and updates in an Alembic upgrade script? for some hours, finally got it done 🤗. Checked yesterday, it works!