question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Saving related model broke in latest release

See original GitHub issue

I have a test that started failing after I updated to the latest 0.7.0 release.

c = await domain.Category(name="Foo", code=123).save()
ws = await domain.Workshop(topic="Topic 1", category=c).save()

assert ws.id == 1
assert ws.topic == "Topic 1"
assert ws.category.name == "Foo"

Simple test, and the error is

E       AssertionError: assert None == 'Foo'
E         +None
E         -'Foo'

I can see that the category gets saved and is also saved in the database for the workshop, but it’s not updated in the model correctly. In the ws.category I see only the id column, and saved = False.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
collerekcommented, Dec 7, 2020

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.

0reactions
collerekcommented, Dec 7, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Saving Your Data - 2.x - CakePHP Cookbook
When working with associated models, it is important to realize that saving model data should always be done by the corresponding CakePHP model....
Read more >
Calling `$model->save()` does not work if the `$model` object ...
The User model that is returned is basically a new instance with a 'name' property. It's not the actual instance representing that row...
Read more >
how to override django save method to update some field ...
Solution One: I think instead of changing in Ledger model, you should change in Expense model, like this: class Expense(models.
Read more >
[FNV] New Vegas Save Related Issues Help Thread - Reddit
Is a common issue with certain save methods in New Vegas. Will often manifest itself as broken quests, infinite loading screens/crash on ...
Read more >
Split and Save Bodies - 2022 - SOLIDWORKS Help
This enables you to save the bodies from a split part to a different folder or with different names to the same folder....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found