MasoniteFramework / orm

Masonite ORM is a beautiful Python ORM. It's also a nearly drop in replacement of the Orator ORM
https://orm.masoniteproject.com
MIT License
160 stars 47 forks source link

TypeError exception when running `seed:run` #851

Closed mehrenreich closed 1 year ago

mehrenreich commented 1 year ago

Describe the bug

When trying to execute a database seeder, I'm running into this error:

masonite-orm seed:run -C config.py -d seeds 

  TypeError

  'NoneType' object is not callable

  at ~/.pyenv/versions/3.10.8/lib/python3.10/site-packages/masoniteorm/seeds/Seeder.py:26 in run_database_seed
      22│ 
      23│         self.ran_seeds.append(database_seeder)
      24│ 
      25│         if not self.dry:
    → 26│             database_seeder(connection=self.connection).run()
      27│ 
      28│     def run_specific_seed(self, seed):
      29│         file_name = f"{self.seed_module}.{seed}"
      30│         database_seeder = pydoc.locate(file_name)

The seeder file:

"""TenantTableSeeder Seeder."""

from masoniteorm.seeds import Seeder
from sih_api.db.models.Tenant import Tenant

class TenantTableSeeder(Seeder):
    def run(self):
        """Run the database seeds."""
        Tenant.create({"name": "test"})

The seeder file, as well as the files for model and migrations, have been created using:

masonite-orm model -m -s -c -d models -D migrations -S seeds -C config.py tenant

The migration went well, so the database config (and connection) is good.

My workplace looks like:

.
├── config.py
├── migrations
│   ├── 2023_05_13_150655_create_tenants_table.py
├── models
│   └── Tenant.py
└── seeds
    └── tenant_table_seeder.py

Database config:

from masoniteorm.connections import ConnectionResolver

DATABASES = {
    "default": "postgres",
    "postgres": {
        "host": "localhost",
        "driver": "postgres",
        "database": "xxx",
        "user": "xxx",
        "password": "xxx",
        "port": 5432,
        "log_queries": True,
    },
}

DB = ConnectionResolver().set_connection_details(DATABASES)

To Reproduce Steps to reproduce the behavior:

  1. Run the commands as seen above

Expected behaviour One new Tenant model with the name test written to the database.

What database are you using?

mehrenreich commented 1 year ago

OK, USAGE says:

  ORM Version: seed:run [-c <...>] [--dry] [-d <...>] [-C <...>] [<table>]

So, when adding the table name (as in SQL), I get:

masonite-orm seed:run -C config.py -d seeds tenants
  ValueError
  Could not find the seeds.tenants_table_seeder.TenantsTableSeeder seeder file

When adding the model name, it's:

 masonite-orm seed:run -C config.py -d seeds Tenant 
  ConfigurationNotFound
  ORM configuration file has not been found in config.database.
mehrenreich commented 1 year ago

Got it, it's working now.

Turned out that I need to explicitly set the DB_CONFIG_PATH var in order to make it work:

DB_CONFIG_PATH=db/config.py masonite-orm seed:run -C db/config.py -d db/seeds Tenant

The -C option seems to be ignored.