使用 Flask SqlAlchemy 创建多条记录时出现完整性错误

Integrity Error when creating multiple records with Flask SqlAlchemy

我有一个函数可以使用 SQL Alchemy

在 Flask 应用程序中创建一些新的数据库条目
def add_volunteer_client_record(volunteer_id, **kwargs):
    try:
        volunteer_client = VolunteerClient(volunteer_id=volunteer_id, **kwargs)

        volunteer_report_action_items = VolunteerReportActionItems(volunteer_client_id = volunteer_client.id)

        db_session.add(volunteer_client) 
        db_session.add(volunteer_report_action_items)
        db_session.commit()

        return volunteer_client
    except IntegrityError as e:
        db_session.rollback()
        message = "Integrity error occurred"
        raise BadRequestError(messages={'volunteer_client': [message]})

volunteer_client 创建正常,但是当 volunteer_report_action_items 添加到会话中时,我收到了 IntegrityError 和我不太明白为什么。

我的模特

class VolunteerClient(Base):
    __tablename__ = 'volunteer_client'

    id = Column(Integer, primary_key=True, autoincrement=True, index=True)
    volunteer_id = Column(Integer, ForeignKey('provider_user.user_id', onupdate='CASCADE', ondelete='RESTRICT'), unique=True)
    client_id = Column(Integer, ForeignKey('user.id', onupdate='CASCADE', ondelete='RESTRICT'), unique=True)
class VolunteerReportActionItems(Base):
    __tablename__ = 'volunteer_report_action_items'

    id = Column(Integer, primary_key=True, autoincrement=True, index=True)
    volunteer_client_id = Column(Integer, ForeignKey('volunteer_client.id', onupdate='CASCADE', ondelete='RESTRICT'))

SQL

CREATE TABLE IF NOT EXISTS public.volunteer_client
(
  id serial PRIMARY KEY,
  volunteer_id integer NOT NULL,
  client_id integer NOT NULL,
  created_by text,
  created_at timestamp NOT NULL DEFAULT now(),
  updated_by text,
  updated_at timestamp NOT NULL DEFAULT now(),
  UNIQUE(volunteer_id, client_id),
  CONSTRAINT fk_volunteer_client_volunteer FOREIGN KEY (volunteer_id)
      REFERENCES public.user (id) MATCH SIMPLE
      ON UPDATE CASCADE
      ON DELETE RESTRICT,
  CONSTRAINT fk_volunteer_client_client FOREIGN KEY (client_id)
      REFERENCES public.user (id) MATCH SIMPLE
      ON UPDATE CASCADE
      ON DELETE RESTRICT
);

ALTER TABLE public.volunteer_client OWNER to navigate;
CREATE TABLE IF NOT EXISTS public.volunteer_report_action_items
(
  id serial PRIMARY KEY,
  volunteer_client_id integer NOT NULL,
  created_by text,
  created_at timestamp NOT NULL DEFAULT now(),
  updated_by text,
  updated_at timestamp NOT NULL DEFAULT now(),
  CONSTRAINT fk_volunteer_report_action_items_volunteer_client FOREIGN KEY (volunteer_client_id)
      REFERENCES public.volunteer_client (id) MATCH SIMPLE
      ON UPDATE CASCADE
      ON DELETE RESTRICT
);

ALTER TABLE public.volunteer_report_action_items OWNER to navigate;

这里有任何帮助或建议都将非常有用。谢谢!

您需要刷新 VolunteerClient 才能访问 VolunteerClient.id

def add_volunteer_client_record(volunteer_id, **kwargs):
    try:
        volunteer_client = VolunteerClient(volunteer_id=volunteer_id, **kwargs)
        db_session.add(volunteer_client)
        db_session.flush() 
        volunteer_report_action_items = VolunteerReportActionItems(volunteer_client_id = volunteer_client.id)
        db_session.add(volunteer_report_action_items)
        db_session.commit()
    ...