I'm trying to setup a post request with SQL Alchemy where I can update entries in a User table. I'm running into an issue where the user_id is violating the non_null constraint
The error:
(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'}
For context, I've set up a One-to-Many relationship with a State table (as the Parent) and the User table (as the Child). Prior to setting up the relationship, the database would create a new user_id upon every entry. After setting up this relationship, I now get this error
here is my relevant code
models.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")
main.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)
I think the issue's cause "id" is primary, so I tried setting Nullable to True:
Column(Integer, primary_key=True, nullable=True),
This doesn't seem like good practice though, and it didn't work regardless, so any advice would be rlly appreciated

idcolumn in theUsertable on the server. The DDL for that column should not be simplyid integer NOT NULL. Feel free to edit your question to show the CREATE TABLE statement that your tool displays.