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

better naming model tables #13

Open
lsloan opened this issue Apr 20, 2023 · 2 comments
Open

better naming model tables #13

lsloan opened this issue Apr 20, 2023 · 2 comments
Assignees

Comments

@lsloan
Copy link
Member

lsloan commented Apr 20, 2023

If I let Django name the DB tables, it prefixes each one with the name of the app. For example, the table for the Assessment class gets named peer_review_data_assessment, which I don't like.

I don't understand why 3/8 of the programmatic table namings don't work. They required me to hard-code them. I'd like an alternate technique that gives better table names programmatically without the occasional unexplained error.

Originally posted by @ssciolla in #11 (comment):

After digging into the import situation a bit, it looks like you are defining db_table in many cases (not all) by using the internal name of the class from canvasapi. It's probably okay, but I guess I'd lean towards hard-coding the names, or letting Django generate them based on the class name, to avoid any possible downstream effects of changes in the library classes or structure.

@lsloan lsloan self-assigned this Apr 20, 2023
@lsloan
Copy link
Member Author

lsloan commented Apr 20, 2023

Code that seems close to working…

# Should this class be abstract?  Is base class correct?
class MyModelBase(models.base.ModelBase):
    def __new__(cls, name, bases, attrs, **kwargs):
        class NewMeta:
            db_table = name

        attrs['Meta'] = NewMeta

        r = super().__new__(cls, name, bases, attrs, **kwargs)
        return r


# Is the base class correct, or should it be models.base.Model?
class MyModel(models.Model, metaclass=MyModelBase):
    class Meta:
        abstract = True


class Course(MyModel):
    # etc.

However, this code gives the error…

django.core.exceptions.FieldError: Local field 'id' in class 'Course' clashes with field of the same name from base class 'MyModel'.

@ssciolla
Copy link
Contributor

ssciolla commented May 1, 2023

Can this be closed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants