2017-02-16 135 views
0

我是一個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看起來類似,但使用子查詢找到外鍵。

我該如何做到「正確」?

+0

此代碼爲我拋出異常,因爲'fk_course'(或任何其他此類屬性)是一種關係,它接受'Course'實例。在你的例子中,你嘗試分配'Query'對象。你能檢查你的表情嗎?他們應該看起來像這樣:'sched.fk_course = session.query(Course).filter(Course.course_number =='CS250')。first()'。 –

+0

我嘗試創建一個課程(等)的實例,它仍然嘗試插入課程表。我不希望這樣 - 我想將course_id插入RoomSchedule表中。 – zenlc2000

回答

0

我仍然不確定答案,但我切換到MongoDB,現在生活變得更加容易。