This commit is contained in:
collerek
2021-02-01 13:09:01 +01:00
parent 04bbe13c09
commit 5a4c42b318
2 changed files with 10 additions and 8 deletions

View File

@ -234,12 +234,13 @@ 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
):
meta.table = sqlalchemy.Table(
table = sqlalchemy.Table(
meta.tablename,
meta.metadata,
*[copy.deepcopy(col) for col in meta.columns],
*meta.constraints,
)
meta.table = table
def update_column_definition(

View File

@ -3,7 +3,7 @@ from typing import Optional
import databases
import pytest
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy import ForeignKeyConstraint, create_engine, inspect
from sqlalchemy.dialects import postgresql
import ormar
@ -11,6 +11,7 @@ from tests.settings import DATABASE_URL
database = databases.Database(DATABASE_URL, force_rollback=True)
metadata = sqlalchemy.MetaData()
engine = sqlalchemy.create_engine(DATABASE_URL, echo=True)
class Artist(ormar.Model):
@ -28,6 +29,7 @@ class Album(ormar.Model):
tablename = "albums"
metadata = metadata
database = database
constraint = [ForeignKeyConstraint(['albums'],['albums.id'])]
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
@ -47,20 +49,19 @@ class Track(ormar.Model):
@pytest.fixture(autouse=True, scope="module")
def create_test_database():
engine = sqlalchemy.create_engine(DATABASE_URL)
if "sqlite" in DATABASE_URL:
with engine.connect() as connection:
connection.execute("PRAGMA foreign_keys = ON;")
metadata.drop_all(engine)
metadata.create_all(engine)
yield
# metadata.drop_all(engine)
metadata.drop_all(engine)
# def test_table_structures():
# col = Album.Meta.table.columns.get('artist')
# breakpoint()
def test_table_structures():
col = Album.Meta.table.columns.get('artist')
inspector = inspect(engine)
col2 = inspector.get_columns('albums')
@pytest.mark.asyncio
async def test_simple_cascade():