-
-
Notifications
You must be signed in to change notification settings - Fork 666
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Order of columns in the table created does not have 'id' first, despite the order in the SQLModel. Looks like it's prioritising fields with sa_column #542
Comments
I've just encountered this problem too - it originates from the way sqlalchemy deals with a My workaround ended up exploiting
Works well enough (negligible perf impact since it's at class generation time), though would be obsoleted by #436 . |
i copy your code and run ,but its not work , resource_data in the first |
Is this still a problem? I cannot reproduce it (sqlmodel 0.0.18, sqlalchemy 2.0.29):
|
I'm running into a similar problem where mixed-in classes have their columns added first. For example I have a class Timestamps:
created_at: datetime = Field(sa_type=DateTime(), nullable=False)
updated_at: datetime = Field(sa_type=DateTime(), nullable=False)
class MyModel(SQLModel, Timestamps, table=True)
id: Optional[int] = Field(default=None, primary_key=True) This generates the sql (sqlite): CREATE TABLE mymodel (
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL,
id INTEGER NOT NULL, -- id is below mixed in fields (other fields in MyModel would appear below this)
PRIMARY KEY (id)
); I tried using My workaround is the following hack where I pop columns off of the Tables and then stick them back on when creating the tables. engine = create_engine(sqlite_url)
def create_db_and_tables():
for table in SQLModel.metadata.sorted_tables:
try:
created_at, updated_at = (
table.columns.get("created_at"),
table.columns.get("updated_at"),
)
table._columns.remove(created_at)
table._columns.remove(updated_at)
table._columns.extend((created_at, updated_at))
except KeyError:
pass
SQLModel.metadata.create_all(engine) |
Hello, I also have the same problem as @prurph. I have also tried using
I see in the sqlalchemy documentation for I am running the following versions.
|
Hi! I had the same issue as @prurph, and I solved it by changing the inheritance order. class Timestamps:
created_at: datetime = Field(sa_type=DateTime(), nullable=False)
updated_at: datetime = Field(sa_type=DateTime(), nullable=False)
class IntegerSQLModel(SQLModel):
id: Optional[int] = Field(default=None, primary_key=True)
class MyModel(Timestamps, IntegerSQLModel, table=True):
some_file: str As a result, the following schema is generated:
I often use separate base classes for primary keys in the form of UUID or int, so this solution works fine for me. |
Any update on this? I am on the same boat. |
First Check
Commit to Help
Example Code
Description
The CREATE table script generated for the model above ends up putting resource_data as the first column, instead of preserving the natural order of 'id' first
This feels unusual when I inspect my postgresql tables in a db tool like pgAdmin.
How do I ensure the table is created with the 'natural' order?
Operating System
macOS
Operating System Details
No response
SQLModel Version
0.0.8
Python Version
3.11.1
Additional Context
No response
The text was updated successfully, but these errors were encountered: