Giter Site home page Giter Site logo

Comments (1)

collerek avatar collerek commented on May 18, 2024 1

Hi, thanks for catching that up - fix that in Model.object.create() but didn't in Model.save() before.

Should be fixed now -> please update to latest version (0.5.1) and give it a try.

Also please notice that in your example the DbModel class is redundant.

  • If there is no Meta class declared on an ormar model it's treated as normal pydantic model and in theory you can inherit from it, but ormar redeclares the fields so there is no much use for this
  • If you declare a Meta and not declare any fields on model - ormar will ad id = Integer(primary_key=True) field for you, as all models requires a pk, and this feature populates columns on through model in ManyToMany relations
  • For now ormar does not support model inheritance - so you cannot get multi-table models etc. For now each ormar.Model is a separate table in db.
  • Therefore all ormar models should inherit directly from ormar.Model and declare an internal Meta class with required params

So something like:

DATABASE_URL = "sqlite:///test.db"
database = databases.Database(DATABASE_URL)
metadata = sqlalchemy.MetaData()


class MainMeta(ormar.ModelMeta):
    metadata = metadata
    database = database

# not that db models should inherit from ormar.Model directly
class PositionOrm(ormar.Model):
    class Meta(MainMeta):
        pass

    name: str = String(primary_key=True, max_length=50)
    x: float = Float()
    y: float = Float()
    degrees: float = Float()


@pytest.fixture(autouse=True, scope="module")
def create_test_database():
    engine = create_engine(DATABASE_URL)
    metadata.create_all(engine)
    yield
    metadata.drop_all(engine)


@pytest.fixture(scope="function")
async def cleanup():
    yield
    async with database:
        await PositionOrm.objects.delete(each=True)


@pytest.mark.asyncio
async def test_creating_a_position(cleanup):
    async with database:
        instance = PositionOrm(
            name="my_pos",
            x=1.0,
            y=2.0,
            degrees=3.0,
        )
        await instance.save()
        assert instance.saved
        assert instance.name == "my_pos"

Now it should pass.

BTW. Thanks for the nice words! 😄 If you like the package consider starring it and spreading the news around - I am trying to get some traction and build a community around it to quicker catch bugs like this one (thanks again for catching!) and maybe also get some contributors for further development.

from ormar.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.