0

I am using Flask and SQLAlchemy. I have a model defined like this

class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)
    name = Column(String(255))
    rank = Column(Integer, nullable=False)
    score = Column(Integer, nullable=False)
    created_at = Column('created_at', DateTime, nullable=False, server_default=func.now())

I run alembic alembic revision --autogenerate -m and it generated a revision file which looks like this

def upgrade() -> None:
op.create_table('users',
                sa.Column('id', sa.Integer(), nullable=False),
                sa.Column('name', sa.String(length=255), nullable=True),
                sa.Column('rank', sa.Integer(), nullable=False),
                sa.Column('score', sa.Integer(), nullable=False),
                sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), 
                nullable=False),
                sa.PrimaryKeyConstraint('id')
                )

Then I run alembic upgrade head and get this error

AttributeError: type object 'User' has no attribute ' name'. Did you 
mean: 'name'?

For reference, I have multiple tables and all others have no issues, they created successfully. What could be going wrong, please ask for more info if needed.

Thanks

pnna
  • 113
  • 1
  • 6
  • You have a trailing whitespace in the revision for `'name '`. I don't know how that got there but it might be relevant – roganjosh Apr 07 '23 at 11:14
  • Are you sure you should have `flask-sqlalchemy` tagged? That's its own library and actually quite significantly different from the setup you have. I have a similar setup to you, with `flask` and `sqlalchemy` separate and my tables include `name` as a field in postgres. I don't know where that whitespace comes from because `alembic` doesn't generate it for me – roganjosh Apr 07 '23 at 11:16
  • @roganjosh the trailing space isn't supposed to be there, I edited the code. Not really sure where things go wrong because I have checked a dozen times. – pnna Apr 07 '23 at 11:38
  • It's hard to really say anything when your original code had artifacts that aren't in what you're actually using. It should have been as simple as copy/paste from the migration; who knows what else might have been obscured? – roganjosh Apr 07 '23 at 12:13

0 Answers0