fix too long fk names

This commit is contained in:
collerek
2021-04-04 17:27:34 +02:00
parent a940fcad6e
commit 593d233a46
4 changed files with 52 additions and 50 deletions

View File

@ -20,36 +20,30 @@ class DataSource(ormar.Model):
class Meta(BaseMeta):
tablename = "datasources"
source_id: int = ormar.Integer(primary_key=True)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=200, unique=True, index=True)
class DataSourceTable(ormar.Model):
class Meta(BaseMeta):
tablename = "datasource_tables"
tablename = "tables"
datasource_table_id: int = ormar.Integer(primary_key=True)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=200, index=True)
data_source: Optional[DataSource] = ormar.ForeignKey(
DataSource,
name="data_source_id",
related_name="datasource_tables",
ondelete="CASCADE",
source: Optional[DataSource] = ormar.ForeignKey(
DataSource, name="source_id", related_name="tables", ondelete="CASCADE",
)
class DataSourceTableColumn(ormar.Model):
class Meta(BaseMeta):
tablename = "datasource_table_columns"
tablename = "columns"
datasource_table_column_id: int = ormar.Integer(primary_key=True)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=200, index=True)
data_type: str = ormar.String(max_length=200)
datasource_table: Optional[DataSourceTable] = ormar.ForeignKey(
DataSourceTable,
name="datasource_table_id",
related_name="datasource_table_columns",
ondelete="CASCADE",
table: Optional[DataSourceTable] = ormar.ForeignKey(
DataSourceTable, name="table_id", related_name="columns", ondelete="CASCADE",
)
@ -69,7 +63,7 @@ async def test_double_nested_reverse_relation():
test_tables = [
{
"name": "test1",
"datasource_table_columns": [
"columns": [
{"name": "col1", "data_type": "test"},
{"name": "col2", "data_type": "test2"},
{"name": "col3", "data_type": "test3"},
@ -77,14 +71,14 @@ async def test_double_nested_reverse_relation():
},
{
"name": "test2",
"datasource_table_columns": [
"columns": [
{"name": "col4", "data_type": "test"},
{"name": "col5", "data_type": "test2"},
{"name": "col6", "data_type": "test3"},
],
},
]
data_source.datasource_tables = test_tables
data_source.tables = test_tables
await data_source.save_related(save_all=True, follow=True)
tables = await DataSourceTable.objects.all()
@ -94,24 +88,14 @@ async def test_double_nested_reverse_relation():
assert len(columns) == 6
data_source = (
await DataSource.objects.select_related(
"datasource_tables__datasource_table_columns"
)
.filter(datasource_tables__name__in=["test1", "test2"], name="local")
await DataSource.objects.select_related("tables__columns")
.filter(tables__name__in=["test1", "test2"], name="local")
.get()
)
assert len(data_source.datasource_tables) == 2
assert len(data_source.datasource_tables[0].datasource_table_columns) == 3
assert (
data_source.datasource_tables[0].datasource_table_columns[0].name == "col1"
)
assert (
data_source.datasource_tables[0].datasource_table_columns[2].name == "col3"
)
assert len(data_source.datasource_tables[1].datasource_table_columns) == 3
assert (
data_source.datasource_tables[1].datasource_table_columns[0].name == "col4"
)
assert (
data_source.datasource_tables[1].datasource_table_columns[2].name == "col6"
)
assert len(data_source.tables) == 2
assert len(data_source.tables[0].columns) == 3
assert data_source.tables[0].columns[0].name == "col1"
assert data_source.tables[0].columns[2].name == "col3"
assert len(data_source.tables[1].columns) == 3
assert data_source.tables[1].columns[0].name == "col4"
assert data_source.tables[1].columns[2].name == "col6"

View File

@ -18,7 +18,7 @@ class Album(ormar.Model):
metadata = metadata
database = database
id: int = ormar.Integer(primary_key=True)
id: int = ormar.Integer(primary_key=True, name="album_id")
name: str = ormar.String(max_length=100)
is_best_seller: bool = ormar.Boolean(default=False)
@ -29,7 +29,7 @@ class Writer(ormar.Model):
metadata = metadata
database = database
id: int = ormar.Integer(primary_key=True)
id: int = ormar.Integer(primary_key=True, name="writer_id")
name: str = ormar.String(max_length=100)
@ -40,11 +40,11 @@ class Track(ormar.Model):
database = database
id: int = ormar.Integer(primary_key=True)
album: Optional[Album] = ormar.ForeignKey(Album)
album: Optional[Album] = ormar.ForeignKey(Album, name="album_id")
title: str = ormar.String(max_length=100)
position: int = ormar.Integer()
play_count: int = ormar.Integer(nullable=True)
written_by: Optional[Writer] = ormar.ForeignKey(Writer)
written_by: Optional[Writer] = ormar.ForeignKey(Writer, name="writer_id")
async def get_sample_data():