Comments (5)
OK,
I wanted to make sure that pre-save signal receive the model with populated defaults and overdid it, sorry :)
Should be fixed in 0.7.1.
Btw. When you want pytest fixture to run only once for whole module (like db creation) you cen set scope=module
option in fixture decorator
@pytest.fixture(autouse=True, scope="module")
def create_test_database():
engine = create_engine(DATABASE_URL)
metadata.create_all(engine)
yield
metadata.drop_all(engine)
That way you don't have to pass the fixture around to test functions.
Also now you run against sqlite but if you intend to run against mysql or postgress you need to actually explicitly connect in test - best as a context manager with nomen omen with
;)
@pytest.mark.asyncio
async def test_model_relationship():
async with db:
async with db.transaction(force_rollback=True):
# rest of the test
It does not affect sqlite buf fixes test for other backends.
from ormar.
Can you share whole sample so I can reproduce?
from ormar.
import databases
import ormar
import pytest
import sqlalchemy as sa
db_url = "sqlite:///./test.db"
engine = sa.create_engine(db_url, connect_args={"check_same_thread": False})
metadata = sa.MetaData(engine)
db = databases.Database(db_url)
class Category(ormar.Model):
class Meta:
tablename = "categories"
metadata = metadata
database = db
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=50, unique=True, index=True)
code: int = ormar.Integer()
class Workshop(ormar.Model):
class Meta:
tablename = "workshops"
metadata = metadata
database = db
id: int = ormar.Integer(primary_key=True)
topic: str = ormar.String(max_length=255, index=True)
category: Union[ormar.Model, Category] = ormar.ForeignKey(
Category, related_name="workshops", nullable=False
)
@pytest.fixture
def create_test_database():
metadata.create_all(engine)
yield
metadata.drop_all(engine)
@pytest.mark.asyncio
async def test_model_relationship(create_test_database):
c = await Category(name="Foo", code=123).save()
ws = await Workshop(topic="Topic 1", category=c).save()
assert ws.id == 1
assert ws.topic == "Topic 1"
assert ws.category.name == "Foo"
I found out that adding await ws.category.load()
fixes the issue, but I think that's a bit redundant to have to do a separate load right after saving the model.
from ormar.
Does this fix the same issue with the .update()
as well? It seems like it's also there. When running model.update()
I have to "reload" the related models as well.
from ormar.
This passes for me:
@pytest.mark.asyncio
async def test_model_relationship():
async with db:
async with db.transaction(force_rollback=True):
cat = await Category(name="Foo", code=123).save()
ws = await Workshop(topic="Topic 1", category=cat).save()
assert ws.id == 1
assert ws.topic == "Topic 1"
assert ws.category.name == "Foo"
ws.topic = 'Topic 2'
await ws.update()
assert ws.id == 1
assert ws.topic == "Topic 2"
assert ws.category.name == "Foo"
So let me know if you still have the same problem.
from ormar.
Related Issues (20)
- use snake case for table names in the database
- Multiple reverse relations not working.
- Add `first_or_none`
- FastpAPI state has not database HOT 4
- How to configure alembic autogenerate with multiple model files HOT 5
- Error awaiting None
- Upgrade to SQLAlchemy 2.0 HOT 5
- Add SELECT DISTINCT feature to queryset
- Failing test: test_weakref_init
- Reverse relation not returned from get_pydantic
- Low performance on select_all() call HOT 1
- Nullable ForeignKey field does not get updated to null
- The future of ORMAR HOT 6
- Support specifying a table comment
- Please support pydantic-2.x HOT 3
- New release with the latest dependency upgrades HOT 2
- Unresolved attribute reference 'objects'
- Model field types and pyright
- v0.20.0 is released as stable instead of beta HOT 3
- Allow tables without primary key
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ormar.