check other backends trial1
This commit is contained in:
@ -1,9 +1,7 @@
|
||||
import copy
|
||||
import logging
|
||||
from typing import Dict, List, Optional, TYPE_CHECKING, Tuple, Type, Union
|
||||
|
||||
import sqlalchemy
|
||||
from sqlalchemy import ForeignKeyConstraint
|
||||
|
||||
from ormar import ForeignKey, Integer, ModelDefinitionError # noqa: I202
|
||||
from ormar.fields import BaseField, ManyToManyField
|
||||
@ -81,10 +79,12 @@ def create_and_append_m2m_fk(
|
||||
model.Meta.tablename + "." + pk_alias,
|
||||
ondelete="CASCADE",
|
||||
onupdate="CASCADE",
|
||||
name=f"fk_{model_field.through.Meta.tablename}_{model.Meta.tablename}"
|
||||
f"_{field_name}_{pk_alias}",
|
||||
),
|
||||
)
|
||||
model_field.through.Meta.columns.append(column)
|
||||
model_field.through.Meta.table.append_column(copy.deepcopy(column))
|
||||
model_field.through.Meta.table.append_column(column)
|
||||
|
||||
|
||||
def check_pk_column_validity(
|
||||
@ -235,18 +235,15 @@ def populate_meta_sqlalchemy_table_if_required(meta: "ModelMeta") -> None:
|
||||
if not hasattr(meta, "table") and check_for_null_type_columns_from_forward_refs(
|
||||
meta
|
||||
):
|
||||
if meta.tablename == 'albums':
|
||||
meta.constraints.append(ForeignKeyConstraint(['artist'],['artists.id'],
|
||||
ondelete='CASCADE',
|
||||
onupdate='CASCADE'))
|
||||
for constraint in meta.constraints:
|
||||
if isinstance(constraint, sqlalchemy.UniqueConstraint):
|
||||
constraint.name = (
|
||||
f"uc_{meta.tablename}_"
|
||||
f'{"_".join([str(col) for col in constraint._pending_colargs])}'
|
||||
)
|
||||
table = sqlalchemy.Table(
|
||||
meta.tablename,
|
||||
meta.metadata,
|
||||
*[copy.deepcopy(col) for col in meta.columns],
|
||||
*meta.constraints,
|
||||
meta.tablename, meta.metadata, *meta.columns, *meta.constraints,
|
||||
)
|
||||
if meta.tablename == 'albums':
|
||||
pass
|
||||
meta.table = table
|
||||
|
||||
|
||||
|
||||
@ -33,6 +33,7 @@ from ormar.models.helpers import (
|
||||
populate_meta_tablename_columns_and_pk,
|
||||
register_relation_in_alias_manager,
|
||||
)
|
||||
from ormar.models.helpers.sqlalchemy import sqlalchemy_columns_from_model_fields
|
||||
from ormar.models.quick_access_views import quick_access_set
|
||||
from ormar.queryset import QuerySet
|
||||
from ormar.relations.alias_manager import AliasManager
|
||||
@ -348,20 +349,25 @@ def copy_and_replace_m2m_through_model(
|
||||
new_meta: ormar.ModelMeta = type( # type: ignore
|
||||
"Meta", (), dict(through_class.Meta.__dict__),
|
||||
)
|
||||
copy_name = through_class.__name__ + attrs.get("__name__", "")
|
||||
copy_through = type(copy_name, (ormar.Model,), {"Meta": new_meta})
|
||||
new_meta.tablename += "_" + meta.tablename
|
||||
# create new table with copied columns but remove foreign keys
|
||||
# they will be populated later in expanding reverse relation
|
||||
if hasattr(new_meta, "table"):
|
||||
del new_meta.table
|
||||
new_meta.columns = [col for col in new_meta.columns if not col.foreign_keys]
|
||||
new_meta.model_fields = {
|
||||
name: field
|
||||
for name, field in new_meta.model_fields.items()
|
||||
if not issubclass(field, ForeignKeyField)
|
||||
}
|
||||
if hasattr(new_meta, "column"):
|
||||
del new_meta.columns
|
||||
_, columns = sqlalchemy_columns_from_model_fields(
|
||||
new_meta.model_fields, copy_through
|
||||
) # type: ignore
|
||||
new_meta.columns = columns
|
||||
populate_meta_sqlalchemy_table_if_required(new_meta)
|
||||
copy_name = through_class.__name__ + attrs.get("__name__", "")
|
||||
copy_through = type(copy_name, (ormar.Model,), {"Meta": new_meta})
|
||||
copy_field.through = copy_through
|
||||
|
||||
parent_fields[field_name] = copy_field
|
||||
|
||||
Reference in New Issue
Block a user