Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ManyToMany incorrectly triggers a ModelDefinitionError #1399

Open
pmdevita opened this issue Oct 20, 2024 · 0 comments
Open

ManyToMany incorrectly triggers a ModelDefinitionError #1399

pmdevita opened this issue Oct 20, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@pmdevita
Copy link
Contributor

Describe the bug
Declaring a ManyToMany error always raises a ModelDefinitionError

To Reproduce

import sqlalchemy
import databases
import ormar

DATABASE_URL = "sqlite:///test.sqlite"

ormar_config = ormar.OrmarConfig(
    metadata=sqlalchemy.MetaData(),
    database=databases.Database(DATABASE_URL),
    engine=sqlalchemy.create_engine(DATABASE_URL)
)


class Author(ormar.Model):
    ormar_config = ormar_config.copy(tablename="authors")

    id = ormar.Integer(primary_key=True)
    name = ormar.String(max_length=100)


class Book(ormar.Model):
    ormar_config = ormar_config.copy(tablename="books")

    id = ormar.Integer(primary_key=True)
    title = ormar.String(max_length=100)
    author = ormar.ManyToMany(Author)
    year = ormar.Integer(nullable=True)


ormar_config.metadata.create_all(ormar_config.engine)

running this should produce this error

ormar.exceptions.ModelDefinitionError: Relation with related_name 'author' leading to model BookAuthor cannot be used on model Book because it's already used by model Author

Expected behavior
The ManyToMany field should be created and usable.

Versions (please complete the following information):

  • Database: SQLite
  • Python version: 3.11
  • ormar version: 0.20.1
  • pydantic version: 2.5.3
  • if applicable fastapi version
@pmdevita pmdevita added the bug Something isn't working label Oct 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant