Fastapi: sqlalchemy.exc.CompileError: Unconsumed column names: items following the sqlalchemy integration tutorial example

Created on 29 Jul 2020  路  3Comments  路  Source: tiangolo/fastapi

Hi! I'm getting this error:

sqlalchemy.exc.CompileError: Unconsumed column names: items

when running a fastapi-users call to register a user: trying to integrate the fastapi sqlalchemy tutorial with the fastapi-users example.

printf '{"email": "[email protected]", "password": "hhhhhhh" }'| http  --follow --timeout 3600 POST localhost:8000/auth/register Content-Type:'application/json'

The sqlalchemy models used are:


from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.types import Numeric
from sqlalchemy.orm import relationship
from fastapi_users.db.sqlalchemy import GUID
from db import Base

class Item(Base):
    __tablename__ = "items"

    id = Column(Integer, primary_key=True, index=True)
    title = Column(String, nullable=True)
    description = Column(String, nullable=True)
    quantita = Column(Numeric(9, 3), nullable=True)
    importo = Column(Numeric(12, 4), nullable=True)
    user_id = Column(GUID(), ForeignKey("users.id", ondelete="CASCADE"))
    user = relationship("UserTable", back_populates="items")

from sqlalchemy import Column, String, DateTime
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship
from fastapi_users.db import SQLAlchemyBaseUserTable
from db import Base

class UserTable(Base, SQLAlchemyBaseUserTable):
    __tablename__ = "users"

    first_name = Column(String(50), nullable=True)
    last_name = Column(String(50), nullable=True)
    created_date = Column(DateTime, default=func.now(), nullable=False)
    items = relationship("Item", back_populates="user", cascade="all, delete")

I found out that the error is depending by the pydantic model used for User in the following line:

items: List[Item] = [] which is triggering the error... why?

class User(models.BaseUser):
    first_name: Optional[str] = None
    last_name: Optional[str] = None
    # items: List[Item] = []

    class Config:
        orm_mode = True

Thank you in advance for you help.

question

All 3 comments

Closed the terminal window (on a Mac). Recreated from scratch the terminal and activated the virtual environment. The isseu disappeared. Closing for good. Sorry for this.

Thanks for reporting back and closing the issue :+1:

Was this page helpful?
0 / 5 - 0 ratings