Coder Social home page Coder Social logo

Migration error about piccolo HOT 10 CLOSED

piccolo-orm avatar piccolo-orm commented on June 22, 2024
Migration error

from piccolo.

Comments (10)

NewDay1313 avatar NewDay1313 commented on June 22, 2024 1

It's because user is a reserved word in Postgres, and having a table with the same name would cause issues

Got it! It explains everything!

Do you think that's the case here?

Definetly It takes place here!

from piccolo.

dantownsend avatar dantownsend commented on June 22, 2024 1

Looks like types.new_class will do the trick:

https://docs.python.org/3/library/types.html#types.new_class

from piccolo.

heliumbrain avatar heliumbrain commented on June 22, 2024 1

Yeah, looks good! I can have a shot at fixing it tomorrow, if you're not on it already @dantownsend :)

from piccolo.

dantownsend avatar dantownsend commented on June 22, 2024 1

Should be fixed now - I've push a new build to PyPI.

from piccolo.

dantownsend avatar dantownsend commented on June 22, 2024

@NewDay1313 Piccolo checks in case a Table has a tablename of 'user'. For example, either of these:

class User(Table):
    ...

class MyTable(Table, tablename='user'):
    ...

It's because user is a reserved word in Postgres, and having a table with the same name would cause issues.

Do you think that's the case here?

from piccolo.

Anonymous1313-tech avatar Anonymous1313-tech commented on June 22, 2024

So will you change that table name in piccolo?

from piccolo.

dantownsend avatar dantownsend commented on June 22, 2024

I don't think any of the builtin migrations for Piccolo use a tablename of user.

Check any migration files which Piccolo has created for you, in case any of the reference a table called user.

If you want you can paste the code here and I'll take a look.

from piccolo.

heliumbrain avatar heliumbrain commented on June 22, 2024

@dantownsend playing around with this last night I noticed that there might be some inconsistency in how the PROTECTED_TABLENAMES check is applied.

in piccolo/table.py:148

tablename = tablename if tablename else _camel_to_snake(cls.__name__)

if tablename in PROTECTED_TABLENAMES:
    raise ValueError(
        f"{tablename} is a protected name, please give your table a "
        "different name."
    )

Can't figure out why, but it seems that tablename will always resolve to cls.__name__

For example, when inheriting from BaseUser:

class User(BaseUser, tablename="my_users"):
    pass

The migrations are succesfully generated (using piccolo migrations new my_app --auto) with manager.add_table("User", tablename="my_users")

How ever, trying to push the migrations with piccolo migrations forwards my_app gives the same error as mentioned in this thread:

user is a protected name, please give your table a different name.

from piccolo.

dantownsend avatar dantownsend commented on June 22, 2024

@heliumbrain You're right, there is an issue. Good catch.

The problem is here:

_Table: t.Type[Table] = type(
add_table.class_name,
(Table,),
{
column._meta.name: column
for column in add_table.columns
},
)
_Table._meta.tablename = add_table.tablename

Piccolo creates a Table class, but only sets the tablename afterwards, so it's triggering this warning when the table is called 'user'. I'll have a read of the docs for type to see if parameters can be passed to it.

from piccolo.

dantownsend avatar dantownsend commented on June 22, 2024

Closing for now - let me know if the problem still persists. Thanks

from piccolo.

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.