Table Column Order Database Incorrect

We are adding new tables to MongoDB at run-time. These are imported to Metabase on /sync_schema. The table column order in Admin is "Database". However, the column order in Admin > Data Model, and therefore the column order when visualizing table, is not the order in the database.

Is there some way to force the column order to be the order of the properties in MongoDB collection? Or, do we need to use the API after import to change the column order type to "Custom" and re-order the columns to desired order?

Thanks in advance.

Diagnostic Info:

{
"browser-info": {
"language": "en-US",
"platform": "Win32",
"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36",
"vendor": "Google Inc."
},
"system-info": {
"file.encoding": "UTF-8",
"java.runtime.name": "OpenJDK Runtime Environment",
"java.runtime.version": "11.0.11+9",
"java.vendor": "AdoptOpenJDK",
"java.vendor.url": "https://adoptopenjdk.net/",
"java.version": "11.0.11",
"java.vm.name": "OpenJDK 64-Bit Server VM",
"java.vm.version": "11.0.11+9",
"os.name": "Linux",
"os.version": "4.19.128-microsoft-standard",
"user.language": "en",
"user.timezone": "GMT"
},
"metabase-info": {
"databases": [
"mongo",
"h2"
],
"hosting-env": "unknown",
"application-database": "postgres",
"application-database-details": {
"database": {
"name": "PostgreSQL",
"version": "13.2 (Debian 13.2-1.pgdg100+1)"
},
"jdbc-driver": {
"name": "PostgreSQL JDBC Driver",
"version": "42.2.18"
}
},
"run-mode": "prod",
"version": {
"date": "2021-04-27",
"tag": "v0.39.1",
"branch": "release-x.39.x",
"hash": "6beba48"
},
"settings": {
"report-timezone": null
}
}
}

Hi @metabasedev01
You are likely seeing this issue:
https://github.com/metabase/metabase/issues/13024 - upvote by clicking :+1: on the first post
Change to any of order order types and then change to "Column order: Database".

Unfortunately, the recommended steps don't correct the issue. The database order in the metabase database is incorrect on import. So when setting it the Order type back to Database the order is not correct.

It looks like we will need to update the order to Custom order, and supply the field order, via the API. Any thoughts on this?

@metabasedev01
The best way to learn the API, is to just use Metabase while having your browser developer Network-tab open and looking at the request, and what data is being send/received.

for anyone looking still looking to fix this with a script that calls the API, here's what we hacked together:
(you can also pretty much one-shot it from chatgpt, but here goes)

import requests
from dotenv import load_dotenv
import os

load_dotenv()
METABASE_URL = os.getenv("METABASE_URL")
METABASE_USER = os.getenv("METABASE_USER")
METABASE_PASSWORD = os.getenv("METABASE_PASSWORD")

# Authenticate and get API token
auth_url = f"{METABASE_URL}/api/session"
auth_payload = {
    "username": METABASE_USER,
    "password": METABASE_PASSWORD,
}
response = requests.post(auth_url, json=auth_payload)
response.raise_for_status()
api_token = response.json()["id"]

# Set headers for authenticated API requests
headers = {
    "Content-Type": "application/json",
    "X-Metabase-Session": api_token,
}

# Get all tables
tables_url = f"{METABASE_URL}/api/table"
tables_response = requests.get(tables_url, headers=headers)
tables_response.raise_for_status()
tables = tables_response.json()

# Iterate through all tables
for table in tables:

    # makes sure to update the table's field_order to "database",
    # by first setting it to "alphabetical" and then back to "database"
    # see bug/issue at https://github.com/metabase/metabase/issues/13024
    table_update_url = f"{METABASE_URL}/api/table/{table['id']}"
    table_update_payload = {
        "field_order": "alphabetical"
    }
    table_update_response = requests.put(table_update_url, json=table_update_payload, headers=headers)
    table_update_response.raise_for_status()

    table_update_payload = {
        "field_order": "database"
    }
    table_update_response = requests.put(table_update_url, json=table_update_payload, headers=headers)
    table_update_response.raise_for_status()
    


    print(f"Updated field order to 'database' for table '{table['name']}'")

print("Finished updating all tables.")