AttributeError: 'SqliteDatabase' object has no attribute 'Infraction'

15 views
Skip to first unread message

Space

unread,
Apr 20, 2021, 9:13:56 PM4/20/21
to peewee-orm
Hey, I was casually trying to do some data creation with peewee, but I got this error.

AttributeError: 'SqliteDatabase' object has no attribute 'Infraction'

Under core/database.py I have:

import logging
import os
from peewee import *
from discord.enums import ExpireBehavior
from playhouse.shortcuts import model_to_dict, dict_to_model 
from playhouse.sqlite_ext import RowIDField
from datetime import datetime
from playhouse.shortcuts import ReconnectMixin
from flask import Flask
from peewee import *

db = SqliteDatabase("data.db", pragmas={'foreign_keys': 1})
logger = logging.getLogger(__name__)

def iter_table(model_dict):
  for key in model_dict:
    if not db.table_exists(key):
     db.connect(reuse_if_open=True)
     db.create_tables([model_dict[key]])
     logger.debug(f"Created table '{key}'")
     db.close()

class BaseModel(Model):
  class Meta:
    database = db

class Infraction(BaseModel):
"""Stores our warnings."""
  id = AutoField()
  userID = TextField()
  reason = TextField()
  typeAction = TextField()
  MuteTimeRelease = TextField(default = None)
  ActiveMute = TextField(default=False)



app = Flask(__name__)

@app.before_request
def _db_connect():
  db.connect()

@app.teardown_request
def _db_close(exc):
  if not db.is_closed():
    db.close()

tables = {"infraction": Infraction}
iter_table(tables)
----------------------------------------------------------------------------------------------------------------------------------

Then under core/functions.py I have:

from datetime import datetime, timedelta
import discord
from core import database


async def addEntry(self, ctx, target, reason, typeAction, MuteRelease = None, ActiveWarn = False):
  database.db.connect(reuse_if_open=True)
  warn: database.db.Infraction = database.db.Infraction.create(userID = target, reason =     reason, typeAction = typeAction, MuteTimeRelease = MuteRelease, ActiveWarn = ActiveWarn)
  warn.save()

  database.db.close()
----------------------------------------------------------------------------------------------------------------------------------

I've tried renaming the SQLite object but I had no luck getting it to recognize that class.


Charles Leifer

unread,
Apr 23, 2021, 11:25:42 AM4/23/21
to peewe...@googlegroups.com
It does seem like you are running into some naming conflict with your modules, rather than this being a peewee bug. I'd suggest stepping back and simplifying your modules and seeing if you can figure out where you've made a mistake...

For what it's worth it looks like you're trying to access "Infraction" as if it were an attribute of the peewee database object, which makes no sense.

Change:

database.db.Infraction

to:

database.Infraction (module . ModelName)

--
You received this message because you are subscribed to the Google Groups "peewee-orm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to peewee-orm+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/peewee-orm/1f3da273-4fc4-450e-8e46-cd075efa19e3n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages