插入外键表

时间:2017-02-16 01:59:38

标签: sqlalchemy

我是一个SQLAlchemy noob试图通过编写一个小房间可用性Web应用程序来学习。我已将表格标准化,因此我有一堆参考表(一周中的几天,房间号,课程编号等)以及具有参考表外键的房间计划表。我最初尝试使用自动功能,但是当它似乎无法工作时,我使用sqlacodegen来创建一个模型,如下所示:

from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base


Base = declarative_base()
metadata = Base.metadata


class Course(Base):
    __tablename__ = 'courses'

    course_id = Column(Integer, primary_key=True, unique=True)
    course_number = Column(String(8), nullable=False, unique=True)


class DayOfWeek(Base):
    __tablename__ = 'day_of_week'

    day_id = Column(Integer, primary_key=True)
    weekday = Column(String(10), nullable=False)


class EndTime(Base):
    __tablename__ = 'end_time'

    end_id = Column(Integer, primary_key=True, unique=True)
    end_time = Column(String(8), nullable=False)


class Quarter(Base):
    __tablename__ = 'quarter'

    quarter_id = Column(Integer, primary_key=True, unique=True)
    quarter_name = Column(String(10), nullable=False, unique=True)


class Room(Base):
    __tablename__ = 'room'

    room_id = Column(Integer, primary_key=True, unique=True)
    room_number = Column(Integer, nullable=False, unique=True)


class RoomSchedule(Base):
    __tablename__ = 'room_schedule'

    schedule_id = Column(Integer, primary_key=True)
    fk_room_id = Column(ForeignKey('room.room_id'), nullable=False, index=True)
    fk_quarter_id = Column(ForeignKey('quarter.quarter_id'), nullable=False, index=True)
    fk_day_id = Column(ForeignKey('day_of_week.day_id'), nullable=False, index=True)
    fk_start_time = Column(ForeignKey('start_time.time_id'), nullable=False, index=True)
    fk_end_time = Column(ForeignKey('end_time.end_id'), index=True)
    fk_course_id = Column(ForeignKey('courses.course_id'), nullable=False, index=True)

    fk_course = relationship('Course')
    fk_day = relationship('DayOfWeek')
    end_time = relationship('EndTime')
    fk_quarter = relationship('Quarter')
    fk_room = relationship('Room')
    start_time = relationship('StartTime')


class StartTime(Base):
    __tablename__ = 'start_time'

    time_id = Column(Integer, primary_key=True, unique=True)
    start = Column(String(6), nullable=False)

问题是当我插入room_schedule表时,它会尝试反向插入到引用表中。我目前的插入尝试如下:

sched = RoomSchedule()
sched.fk_course = session.query(Course.course_id).filter(Course.course_number =='CS250')
sched.fk_day = session.query(DayOfWeek.day_id).filter(DayOfWeek.weekday =='Tuesday')
sched.fk_end_time = session.query(EndTime.end_id).filter(EndTime.end_time == '22:00')
sched.fk_quarter = session.query(Quarter.quarter_id).filter(Quarter.quarter_name=='1701')
sched.fk_room = session.query(Room.room_id).filter(Room.room_number=='416')
sched.fk_start_time = session.query(StartTime.time_id).filter(StartTime.start=='7:30')
session.add(sched)
session.commit()

我也尝试使用各个类构造函数来提供外键。

如果我试图用直接的Python解决这个问题,我会这样做:

def insert_room_busy(con, room_num, quarter_name, day_name, start_time, end_time, course_number):
    insert_data = 'insert into room_schedule (fk_room_id, fk_quarter_id, fk_day_id, fk_start_time, fk_end_time, fk_course_id) ' \
                  'values( (select room_id from room where room_number= %s), ' \
                  '(select quarter_id from quarter where quarter_name= %s), ' \
                  '(select day_id from day_of_week where weekday= %s), ' \
                  '(select time_id from start_time where start= %s), ' \
                  '(select end_id from end_time where end_time= %s), ' \
                  '(select course_id from courses where course_number= %s));'
    cursor = con.cursor()
    cursor.execute(insert_data, (room_num, quarter_name, day_name, start_time, end_time, course_number))

直接SQL看起来很相似,使用子查询来查找外键。

我如何做到这一点"对"方式是什么?

1 个答案:

答案 0 :(得分:0)

我仍然不确定答案,但我转而使用MongoDB,现在生活更轻松。