Postgres SQL 内部服务器错误:ID 视为非空

问题描述 投票:0回答:1

我正在尝试使用 SQL Alchemy 设置发布请求,我可以在其中更新用户表中的条目。我遇到了 user_id 违反 non_null 约束的问题

错误:

(pg8000.exceptions.DatabaseError) {'S': 'ERROR', 'V': 'ERROR', 'C': '23502', 'M': 'null value in column "id" of relation "User" violates not-null constraint', 'D': 'Failing row contains (null, string, string, string, string, string, CA, string, string, string, string, 0, Temp, Temp, Temp, t, 2024-06-24 11:55:55.40777, 2024-06-24 11:55:55.40777, t).', 's': 'public', 't': 'User', 'c': 'id', 'F': 'execMain.c', 'L': '2009', 'R': 'ExecConstraints'}

enter image description here

对于上下文,我设置了与状态表(作为父项)和用户表(作为子项)的一对多关系。在建立关系之前,数据库会在每个条目上创建一个新的 user_id。设置此关系后,我现在收到此错误

这是我的相关代码

模型.py

class State(Base):
    __tablename__ = 'state'

    state_code = Column(String(2), primary_key=True)
    state_Name = Column(String(150), nullable=False)

    #relationship 
    # user_id = Column(Integer, ForeignKey('User.user_ID'))
    children = relationship("User", back_populates="parent")
    
class User(Base):
    __tablename__ = 'User'

    id = Column(Integer, primary_key=True)
    ### - UI
    first_name = Column(String(50))
    last_name = Column(String(50))
    address_line_1 = Column(String(100))
    address_line_2 = Column(String(100))
    city = Column(String(50))
    
    zip = Column(String(10))
    login_name = Column(String(100))
    email = Column(String(100), nullable=False)
    mobile_phone_num = Column(String(15))
    grade_level = Column(Integer)
    has_adhd = Column(Boolean)
    ### - UI

    ### - Google Auth
    google_id = Column(String(100))
    google_name = Column(String(100))
    google_profile_url = Column(String(255))
   

    is_active = Column(Boolean, nullable=False) #Default is True
    creation_date = Column(DateTime, nullable=False) # Current date user logs in, hardcoded
    last_update_date = Column(DateTime, nullable=False) # Current date user logs off, hardcoded
    
    #Relationships
    state_code = Column(String(2), ForeignKey('state.state_code'))
    parent = relationship("State", back_populates="children")

主.py

##Schema##
class StateBase(BaseModel):
    state_name: str
class UserBase(BaseModel):
    first_name: str
    last_name: str
    address_line_1: str
    address_line_2: str
    city: str
    state_code: str
    zip: str
    login_name: str
    email: str
    mobile_phone_num: str
    grade_level: int
    has_adhd: bool

    google_id: str
    google_name: str
    google_profile_url: str
    
    is_active: bool
    creation_date: datetime
    last_update_date: datetime

## Cognifin
@app.post("/State")
async def create_state(state:StateBase, db: db_dependency):
    db_state = models.State(
        State_name = state.State_name
        )
    db.add(db_state)
    db.commit()
    db.refresh(db_state)

@app.post("/User")
async def create_user(user:UserBase, db: db_dependency):
    
    db_user = models.User(
        ##UI Elements
        first_name = user.first_name,
        last_name = user.last_name,
        address_line_1 = user.address_line_1,
        address_line_2 = user.address_line_2,
        city = user.city,
        state_code = user.state_code,
        zip = user.zip,
        login_name = user.login_name,
        email = user.email,
        mobile_phone_num = user.mobile_phone_num,
        grade_level = user.grade_level,
        has_adhd = user.has_adhd,

        ### - Google Auth
        google_id = "Temp",
        google_name = "Temp",
        google_profile_url = "Temp",
        ##UI backend
        is_active = True,
        creation_date = datetime.now(),
        #DDBL CHECK WITH KRISHNA
        last_update_date = datetime.now()
        )
    db.add(db_user)
    db.commit()
    db.refresh(db_user)

我认为问题的原因“id”是主要的,所以我尝试将 Nullable 设置为 True:

Column(Integer, primary_key=True, nullable=True),

但这似乎不是一个好的做法,而且无论如何它都不起作用,所以任何建议将不胜感激

python postgresql sqlalchemy fastapi internal-server-error
1个回答
0
投票

使您的 id 成为身份栏:

id = Column(Integer, primary_key=True)

应该变成:

id = Column(Integer, Identity(), primary_key=True)

请参阅手册了解更多说明。

不要使用序列号或序列,让数据库使用

identity
为您处理此问题。

© www.soinside.com 2019 - 2024. All rights reserved.