You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I used the GitHub search to find a similar question and didn't find it.
I searched the SQLModel documentation, with the integrated search.
I already searched in Google "How to X in SQLModel" and didn't find any information.
I already read and followed all the tutorial in the docs and didn't find an answer.
I already checked if it is not related to SQLModel but to Pydantic.
I already checked if it is not related to SQLModel but to SQLAlchemy.
Commit to Help
I commit to help with one of those options 👆
Example Code
"""Minimal reproducible example for SQLModel dynamic creation issue.Problem: Creating a SQLModel dynamically with pydantic.create_model() and multiple inheritancefails on second call with "Table already defined" error, even when trying to pass extend_existing=True.Run this file to see the error: python sqlmodel_issues.py"""fromtypingimportOptionalfromdatetimeimportdatetimefrompydanticimportBaseModel, create_model, FieldasPydanticFieldfromsqlmodelimportSQLModel, Field# ============================================================================# Step 1: Base Model (static, shared across all dynamic models)# ============================================================================classGenericRecordModel(SQLModel):
"""Base SQLModel with standard database fields for all tables."""id: int=Field(primary_key=True)
azureDestination: Optional[str] =Field(default=None, max_length=100)
timestamp: Optional[datetime] =Field(default=None)
# ============================================================================# Step 2: Simulated LLM Model (in real app, this comes from JSON Schema)# ============================================================================classSimulatedLLMModel(BaseModel):
""" In production, this is generated from JSON Schema using json-schema-to-pydantic. Contains validation constraints (maxLength, etc.) that must be preserved. """usdot: Optional[str] =PydanticField(default=None, max_length=50)
vehicleType: Optional[str] =PydanticField(default=None, max_length=50)
# ============================================================================# Step 3: Dynamic Model Creation Function# ============================================================================defcreate_dynamic_table_model(table_name: str, llm_model: type[BaseModel]) ->type[SQLModel]:
""" Create a SQLModel dynamically with multiple inheritance. Goal: Combine llm_model validation + GenericRecordModel fields + SQLModel ORM Issue: Second call fails with "Table 'table_name' is already defined" error. """# CURRENT ATTEMPT - FAILS ON SECOND CALLoutput_model=create_model(
table_name, # Model name = table name__base__=(llm_model, GenericRecordModel, SQLModel), # Multiple inheritance__cls_kwargs__={"table": True} # Enable SQLModel table mode
)
# Tried setting __table_args__ here, but error occurs DURING create_model()# output_model.__table_args__ = {"extend_existing": True} # ❌ Too latereturnoutput_model# ============================================================================# Step 4: Demonstrate the Problem# ============================================================================if__name__=="__main__":
print("="*80)
print("SQLModel Dynamic Creation Issue - Minimal Reproducible Example")
print("="*80)
print()
table_name="ge_snapshot_side_truck"# First call - works fineprint("Step 1: Creating model for the FIRST time...")
try:
model1=create_dynamic_table_model(table_name, SimulatedLLMModel)
print(f"✅ SUCCESS: Model '{model1.__name__}' created")
print(f" Table: {getattr(model1, '__tablename__', 'N/A')}")
print(f" Fields: {list(model1.model_fields.keys())}")
print()
exceptExceptionase:
print(f"❌ FAILED: {e}")
print()
# Second call - FAILSprint("Step 2: Creating the SAME model again (simulates hot reload / test re-run)...")
try:
model2=create_dynamic_table_model(table_name, SimulatedLLMModel)
print(f"✅ SUCCESS: Model '{model2.__name__}' created")
print(f" This should work but currently fails!")
print()
exceptExceptionase:
print(f"❌ FAILED with error:")
print(f" {type(e).__name__}: {e}")
print()
print("This is the problem we need to solve!")
print()
print("="*80)
print("Expected Behavior:")
print("="*80)
print("Both calls should succeed. The second call should either:")
print("1. Reuse the existing table definition (extend_existing=True)")
print("2. Or properly override it without errors")
print()
print("Question: How to properly pass extend_existing=True when using")
print(" pydantic.create_model() with SQLModel and multiple inheritance?")
print("="*80)
Description
SQLModel Dynamic Creation: Is Multiple Inheritance the Right Approach?
Problem Overview
I need to dynamically generate SQLModel classes that combine:
Validation fields from a JSON Schema (dynamic, stored in database)
Common database fields (id, timestamp, etc.) - shared across all tables
SQLModel ORM functionality for database persistence
My current approach uses multiple inheritance with pydantic.create_model(), but I'm not sure if this is the correct pattern.
I'm encountering this error when recreating the same model (tests, hot reload):
sqlalchemy.exc.InvalidRequestError: Table 'my_table_name' is already defined for this MetaData instance.
Specify 'extend_existing=True' to redefine options and columns on an existing Table object.
However, my main concern is: Am I using the right architecture to merge these two models?
Current Code
Base Model (Static)
fromtypingimportOptionalfromdatetimeimportdatetimefromsqlmodelimportSQLModel, FieldclassGenericRecordModel(SQLModel):
"""Base SQLModel with standard database fields."""id: int=Field(primary_key=True)
azureDestination: Optional[str] =Field(default=None, max_length=100)
timestamp: Optional[datetime] =Field(default=None)
Dynamic Model Creation
frompydanticimportBaseModel, create_modelfromsqlmodelimportSQLModelfromjson_schema_to_pydanticimportcreate_modelasjson_schema_create_modeldefcreate_dynamic_sqlmodel(table_name: str, json_schema: dict) ->type[SQLModel]:
""" Creates a SQLModel dynamically from JSON Schema with multiple inheritance. Goal: OutputModel should inherit from: - llm_model (Pydantic validation from JSON Schema) - GenericRecordModel (database fields: id, timestamp, etc.) - SQLModel (ORM functionality) """# Step 1: Generate Pydantic model from JSON Schema# This model contains validated fields with constraints (maxLength, etc.)llm_model=json_schema_create_model(json_schema)
# Step 2: Create SQLModel with multiple inheritance# Problem: How to properly pass extend_existing=True?output_model=create_model(
table_name, # Model name = table name__base__=(llm_model, GenericRecordModel, SQLModel),
__cls_kwargs__={"table": True}
)
returnoutput_model
Example Usage
# JSON Schema from databaseschema= {
"type": "object",
"properties": {
"usdot": {"type": ["string", "null"], "maxLength": 50},
"vehicleType": {"type": ["string", "null"], "maxLength": 50}
}
}
# First call - works finemodel1=create_dynamic_sqlmodel("ge_snapshot_side_truck", schema)
# Second call - FAILS with "Table already defined" errormodel2=create_dynamic_sqlmodel("ge_snapshot_side_truck", schema)
What I've Tried
Attempt 1: Setting __table_args__ after creation
output_model=create_model(
table_name,
__base__=(llm_model, GenericRecordModel, SQLModel),
__cls_kwargs__={"table": True}
)
output_model.__table_args__= {"extend_existing": True} # ❌ Too late - error occurs during create_model()
Result: Error occurs duringcreate_model() execution, so this line is never reached.
Attempt 2: Passing __table_args__ in __cls_kwargs__
Result: Still fails with the same error. __table_args__ seems to be ignored when passed this way.
Attempt 3: Passing __table_args__ as field definition
output_model=create_model(
table_name,
__base__=(llm_model, GenericRecordModel, SQLModel),
__cls_kwargs__={"table": True},
__table_args__={"extend_existing": True} # As a field, not cls_kwargs
)
Result: Same error persists.
Core Questions
1. Is multiple inheritance the right pattern here?
I'm using __base__=(llm_model, GenericRecordModel, SQLModel) to merge:
Fields from the dynamically generated llm_model (with Pydantic validation constraints)
Fields from GenericRecordModel (id, timestamp, etc.)
SQLModel ORM capabilities
Is this the recommended way to combine a dynamic Pydantic model with a static SQLModel base?
Alternative approaches I've considered:
Manually copying fields from llm_model to a new SQLModel (loses metadata?)
Using field definitions instead of inheritance: **llm_model.model_fields
Creating the model differently (but how?)
2. How to properly merge GenericRecordModel + LLM Model?
My GenericRecordModel is a SQLModel (not table=True) that I want to be inherited by all dynamic models. Is this correct, or should it be:
A regular Pydantic BaseModel?
A mixin without SQLModel?
Defined differently?
3. The extend_existing=True issue
As a secondary issue, I can't figure out how to pass extend_existing=True to SQLModel when using pydantic.create_model():
❌ Setting __table_args__ after creation is too late (error occurs during create_model())
❌ Passing in __cls_kwargs__ doesn't work
❌ Passing as a field definition doesn't work
But more importantly: Is the multiple inheritance approach fundamentally correct, or am I solving the problem the wrong way?
What I'm Really Asking
I feel like I might be fighting against the framework instead of using it correctly.
What's the proper/idiomatic way to:
Generate a Pydantic model from JSON Schema (with all validation constraints)
Add common database fields (id, timestamp, etc.)
Make it a SQLModel table for persistence
Allow the model to be recreated (tests, hot reload)
Any guidance on the architecture would be greatly appreciated! 🙏
Environment
Python: 3.11+
Pydantic: v2.x
SQLModel: latest
SQLAlchemy: 2.x (via SQLModel)
json-schema-to-pydantic: latest
Reproducible Example
See attached sqlmodel_issues.py for a minimal standalone example that demonstrates the issue.
Operating System
Windows
Operating System Details
SQLModel Dynamic Creation: Is Multiple Inheritance the Right Approach?
Problem Overview
I need to dynamically generate SQLModel classes that combine:
Validation fields from a JSON Schema (dynamic, stored in database)
Common database fields (id, timestamp, etc.) - shared across all tables
SQLModel ORM functionality for database persistence
My current approach uses multiple inheritance with pydantic.create_model(), but I'm not sure if this is the correct pattern.
I'm encountering this error when recreating the same model (tests, hot reload):
sqlalchemy.exc.InvalidRequestError: Table 'my_table_name' is already defined for this MetaData instance.
Specify 'extend_existing=True' to redefine options and columns on an existing Table object.
However, my main concern is: Am I using the right architecture to merge these two models?
Current Code
Base Model (Static)
fromtypingimportOptionalfromdatetimeimportdatetimefromsqlmodelimportSQLModel, FieldclassGenericRecordModel(SQLModel):
"""Base SQLModel with standard database fields."""id: int=Field(primary_key=True)
azureDestination: Optional[str] =Field(default=None, max_length=100)
timestamp: Optional[datetime] =Field(default=None)
Dynamic Model Creation
frompydanticimportBaseModel, create_modelfromsqlmodelimportSQLModelfromjson_schema_to_pydanticimportcreate_modelasjson_schema_create_modeldefcreate_dynamic_sqlmodel(table_name: str, json_schema: dict) ->type[SQLModel]:
""" Creates a SQLModel dynamically from JSON Schema with multiple inheritance. Goal: OutputModel should inherit from: - llm_model (Pydantic validation from JSON Schema) - GenericRecordModel (database fields: id, timestamp, etc.) - SQLModel (ORM functionality) """# Step 1: Generate Pydantic model from JSON Schema# This model contains validated fields with constraints (maxLength, etc.)llm_model=json_schema_create_model(json_schema)
# Step 2: Create SQLModel with multiple inheritance# Problem: How to properly pass extend_existing=True?output_model=create_model(
table_name, # Model name = table name__base__=(llm_model, GenericRecordModel, SQLModel),
__cls_kwargs__={"table": True}
)
returnoutput_model
Example Usage
# JSON Schema from databaseschema= {
"type": "object",
"properties": {
"usdot": {"type": ["string", "null"], "maxLength": 50},
"vehicleType": {"type": ["string", "null"], "maxLength": 50}
}
}
# First call - works finemodel1=create_dynamic_sqlmodel("ge_snapshot_side_truck", schema)
# Second call - FAILS with "Table already defined" errormodel2=create_dynamic_sqlmodel("ge_snapshot_side_truck", schema)
What I've Tried
Attempt 1: Setting __table_args__ after creation
output_model=create_model(
table_name,
__base__=(llm_model, GenericRecordModel, SQLModel),
__cls_kwargs__={"table": True}
)
output_model.__table_args__= {"extend_existing": True} # ❌ Too late - error occurs during create_model()
Result: Error occurs duringcreate_model() execution, so this line is never reached.
Attempt 2: Passing __table_args__ in __cls_kwargs__
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
SQLModel Dynamic Creation: Is Multiple Inheritance the Right Approach?
Problem Overview
I need to dynamically generate SQLModel classes that combine:
My current approach uses multiple inheritance with
pydantic.create_model(), but I'm not sure if this is the correct pattern.I'm encountering this error when recreating the same model (tests, hot reload):
However, my main concern is: Am I using the right architecture to merge these two models?
Current Code
Base Model (Static)
Dynamic Model Creation
Example Usage
What I've Tried
Attempt 1: Setting
__table_args__after creationResult: Error occurs during
create_model()execution, so this line is never reached.Attempt 2: Passing
__table_args__in__cls_kwargs__Result: Still fails with the same error.
__table_args__seems to be ignored when passed this way.Attempt 3: Passing
__table_args__as field definitionResult: Same error persists.
Core Questions
1. Is multiple inheritance the right pattern here?
I'm using
__base__=(llm_model, GenericRecordModel, SQLModel)to merge:llm_model(with Pydantic validation constraints)GenericRecordModel(id, timestamp, etc.)Is this the recommended way to combine a dynamic Pydantic model with a static SQLModel base?
Alternative approaches I've considered:
llm_modelto a new SQLModel (loses metadata?)**llm_model.model_fields2. How to properly merge GenericRecordModel + LLM Model?
My
GenericRecordModelis a SQLModel (nottable=True) that I want to be inherited by all dynamic models. Is this correct, or should it be:3. The
extend_existing=TrueissueAs a secondary issue, I can't figure out how to pass
extend_existing=Trueto SQLModel when usingpydantic.create_model():__table_args__after creation is too late (error occurs duringcreate_model())__cls_kwargs__doesn't workBut more importantly: Is the multiple inheritance approach fundamentally correct, or am I solving the problem the wrong way?
What I'm Really Asking
I feel like I might be fighting against the framework instead of using it correctly.
What's the proper/idiomatic way to:
Any guidance on the architecture would be greatly appreciated! 🙏
Environment
Reproducible Example
See attached
sqlmodel_issues.pyfor a minimal standalone example that demonstrates the issue.Operating System
Windows
Operating System Details
SQLModel Dynamic Creation: Is Multiple Inheritance the Right Approach?
Problem Overview
I need to dynamically generate SQLModel classes that combine:
My current approach uses multiple inheritance with
pydantic.create_model(), but I'm not sure if this is the correct pattern.I'm encountering this error when recreating the same model (tests, hot reload):
However, my main concern is: Am I using the right architecture to merge these two models?
Current Code
Base Model (Static)
Dynamic Model Creation
Example Usage
What I've Tried
Attempt 1: Setting
__table_args__after creationResult: Error occurs during
create_model()execution, so this line is never reached.Attempt 2: Passing
__table_args__in__cls_kwargs__Result: Still fails with the same error.
__table_args__seems to be ignored when passed this way.Attempt 3: Passing
__table_args__as field definitionResult: Same error persists.
Core Questions
1. Is multiple inheritance the right pattern here?
I'm using
__base__=(llm_model, GenericRecordModel, SQLModel)to merge:llm_model(with Pydantic validation constraints)GenericRecordModel(id, timestamp, etc.)Is this the recommended way to combine a dynamic Pydantic model with a static SQLModel base?
Alternative approaches I've considered:
llm_modelto a new SQLModel (loses metadata?)**llm_model.model_fields2. How to properly merge GenericRecordModel + LLM Model?
My
GenericRecordModelis a SQLModel (nottable=True) that I want to be inherited by all dynamic models. Is this correct, or should it be:3. The
extend_existing=TrueissueAs a secondary issue, I can't figure out how to pass
extend_existing=Trueto SQLModel when usingpydantic.create_model():__table_args__after creation is too late (error occurs duringcreate_model())__cls_kwargs__doesn't workBut more importantly: Is the multiple inheritance approach fundamentally correct, or am I solving the problem the wrong way?
What I'm Really Asking
I feel like I might be fighting against the framework instead of using it correctly.
What's the proper/idiomatic way to:
Any guidance on the architecture would be greatly appreciated! 🙏
Environment
Reproducible Example
See attached
sqlmodel_issues.pyfor a minimal standalone example that demonstrates the issue.SQLModel Version
0.0.25
Python Version
Python 3.12.9
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions