sqlalchemy / sqlalchemy/alembic

Add support for preserving field order?

Open
#1,539 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

mysql op directives use case
Dominant language
Python
Stars
4.4k
Forks
375
PR merge metrics
No merged PRs in 30d

Description

Describe the use case

As far as I can tell, there doesn't seem to be a way to specify field order when adding a new column? Ive got an inherited model where it would be nice if any new fields that I create via an autogenerated revision would be entered into the database before the base classes fields.

I know I could manually add an execute after adding it to alter the table to insert it after another field but that sounds tedious to enforce and a way to preserve field order would be useful.

Databases / Backends / Drivers targeted

mysql in this example

Example Use

from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.sql import func
from sqlalchemy.dialects.mysql import TIMESTAMP

class MyModel(MyBase):
   existing_field = ...
   new_field = ...

class MyBase(DeclarativeBase):
    created = Column(TIMESTAMP(fsp=2), server_default=func.now(), nullable=False)
    modified = Column(TIMESTAMP(fsp=2), onupdate=func.now(), nullable=False)

The new_field will be created at the end of the table

existing_field|created               |modified              |new_field|
-----------+----------------------+----------------------+------------+
          0|2017-02-08 16:38:14.65|2017-02-08 16:38:14.65|            |

where I'd like to have a way to insert it before a field

existing_field|new_field|created               |modified              |
-----------+------------+----------------------+----------------------+
          0|            |2017-02-08 16:38:14.65|2017-02-08 16:38:14.65|

Have a nice day!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how autogenerated revisions add new columns and how the MySQL backend handles column order. Compare the requested inherited-model ordering with the current behavior; done means a supported way to place a new field before an existing field while preserving current behavior for other databases.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, python, sqlalchemy
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.