class Answer(db.Model): id = db.Column(db.Integer, primary_key=True) # 질문 모델과 연결하기 위한 question_id. 어떤 질문에 대한 답변인지 표시하기 위해 foreign key(외부 키) 이용 # db.ForeignKey(연결할 모델의 속성명, 삭제 연동 설정.) # ondelete='CASCADE' : DB에서 쿼리를 이용하여 질문을 삭제하면 해당 질문에 대한 답변도 함께 삭제 question_id = db.Column(db.Integer, db.ForeignKey('question.id', ondelete='CASCADE')) # 답변 모델에서 질문 모델을 참조하기 위해 추가. # db.relationship(참조할 모델명, 역참조 설정) - 역참조 : 질문에서 답변을 참조하는 것 question = db.relationship('Question', backref=db.backref('answer_set')) content = db.Column(db.Text(), nullable=False) create_date = db.Column(db.DateTime(), nullable=False) modify_date = db.Column(db.DateTime(), nullable=True) voter = db.relationship('User', secondary=answer_voter, backref=db.backref('answer_voter_set')) # answer model에 field 추가 user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), nullable=False) user = db.relationship('User', backref=db.backref('answer_set'))
class Answer(db.Model): id = db.Column(db.Integer, primary_key=True) # 기존 모델과 연동된 속성인 question_id는 foreign key로 설정된다. # ondelete는 삭제시의 설정에 대한 옵션인데, 'CASCADE'로 설정하게 되면 question 데이터 삭제 시에 이와 연동된 answer 데이터도 삭제된다. question_id = db.Column(db.Integer, db.ForeignKey('question.id', ondelete='CASCADE')) # answer 객체에서 question 객체를 상속 # db.relationship('참조할 모델명', backref=역참조 설정 # 역참조: 질문에서 답변을 참조하는 것 - 하나의 질문에 여러개의 답변이 달릴 수 있는데, 질문에서 이 답변들을 참조하는 것 question = db.relationship('Question', backref=db.backref('answer_set')) content = db.Column(db.Text(), nullable=False) create_date = db.Column(db.DateTime(), nullable=False) # user 객체의 id와 연결 user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), nullable=False) user = db.relationship('User', backref=db.backref('answer_set')) # 수정일자 컬럼 modify_date = db.Column(db.DateTime(), nullable=True) # 추천인 voter = db.relationship('User', secondary=answer_voter, backref=db.backref('answer_voter_set'))
class Question(db.Model): id = db.Column(db.Integer, primary_key=True) subject = db.Column(db.String(200), nullable=False) content = db.Column(db.Text(), nullable=True) create_day = db.Column(db.DateTime(), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), nullable=False) user = db.relationship('User', backref=db.backref('question_set')) modify_date = db.Column(db.DateTime(), nullable=True)
class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(150), unique=True, nullable=False) password = db.Column(db.String(200), nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) intro = db.Column(db.Text(), nullable=True) image_path = db.Column(db.String(300), nullable=True) create_date = db.Column(db.DateTime(), nullable=False) modify_date = db.Column(db.DateTime(), nullable=True)
class Userinfo(db.Model): id = db.Column(db.Integer, primary_key=True) pw = db.Column(db.Integer, nullable=False) name = db.Column(db.String(200), nullable=False) age = db.Column(db.Integer, nullable=False) address = db.Column(db.Text(), nullable=False) birthday = db.Column(db.DateTime(), nullable=False) gender = db.Column(db.Integer, nullable=False) # 남=1, 여=2 create_date = db.Column(db.DateTime(), nullable=False)
class Question(db.Model): id = db.Column(db.Integer, primary_key=True) subject = db.Column(db.String(200), nullable=False) content = db.Column(db.Text(), nullable=False) create_date = db.Column(db.DateTime(), nullable=False) modify_date = db.Column(db.DateTime(), nullable=True) user_id = db.Column(db.Integer, db.ForeignKey("user.id", ondelete="CASCADE"), nullable=False) user = db.relationship("User", backref=db.backref("question_set"))
class Answer(db.Model): id = db.Column(db.Integer, primary_key=True) question_id = db.Column(db.Integer, db.ForeignKey('question.id', ondelete='CASCADE')) question = db.relationship('Question', backref=db.backref('answer_set')) content = db.Column(db.Text(), nullable=False) create_date = db.Column(db.DateTime(), nullable=False) modify_date = db.Column(db.DateTime(), nullable=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), nullable=False) user = db.relationship('User', backref=db.backref('answer_set'))
class Question(db.Model): id = db.Column(db.Integer, primary_key = True) subject = db.Column(db.String(200), nullable=False) content = db.Column(db.Text(), nullable=False) create_date = db.Column(db.DateTime(), nullable=False) # (p.162) User 모델 데이터의 id 값을 Question 모델에 포함시킴. #db.ForeignKey : 다른 모델과 연결. ondelete='CASCADE': User 모델 데이터가 삭제되면 Question 모델 데이터도 삭제됨. user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), nullable=False) user = db.relationship('User', backref = db.backref('question_set')) # p.182 nullable=True!! modify_date = db.Column(db.DateTime(), nullable=True) # p.210 Question 모델에 voter 필드 추가하기 voter = db.relationship('User', secondary=question_voter, backref = db.backref('question_voter_set'))
class Comment(db.Model): id = db.Column(db.Integer, primary_key=True) #댓글 고유번호 user_id = db.Column(db.Integer, db.ForeignKey( 'user.id', ondelete = 'CASCADE'), nullable=False) #댓글 작성자(User 모델과 관계를 가짐) user = db.relationship('User', backref=db.backref('comment_set')) content = db.Column(db.Text(), nullable=False) #댓글 내용 create_date = db.Column(db.DateTime(), nullable=False) #댓글 작성 일시 modify_date = db.Column(db.DateTime()) #질문에 댓글을 작성하면 question_id 필드에 값이 저장되고, 답변에 댓글이 작성되면 answer_id 필드에 값이 저장된다. question_id = db.Column(db.Integer, db.ForeignKey( 'question.id', ondelete='CASCADE'), nullable=True) #댓글의 질문(Questioin 모델과 관계를 가짐) question = db.relationship('Question', backref=db.backref('comment_set')) answer_id = db.Column(db.Integer, db.ForeignKey('answer.id', ondelete='CASCADE'), nullable=True) #댓글의 답변(Answer 모델과 관계를 가짐) answer = db.relationship('Answer', backref=db.backref('comment_set'))
class Question(db.Model): #nullable 비어있는값 들어올수없음 의미 디폴드값은 true id = db.Column(db.Integer, primary_key=True) #primary_key 중복 안됌,integer id숫자값 subject = db.Column(db.String(200), nullable=False) #string제목처럼 제한된 글자수 content = db.Column(db.Text(), nullable=False) #text글자수 제한 없음 create_date = db.Column(db.DateTime(), nullable=False) #날짜와 시간 user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), nullable=True) user = db.relationship('User', backref=db.backref('question_set')) modify_date = db.Column(db.DateTime(), nullable=True) voter = db.relationship('User', secondary=question_voter, backref=db.backref('question_voter_set'))
class Apireq(db.Model): #기본키 id = db.Column(db.Integer, primary_key=True) subject = db.Column(db.String(200), nullable=False) service_key = db.Column(db.String(100), nullable=False) target_url = db.Column(db.String(200), nullable=False) parameter = db.Column(db.String(200), nullable=False) result_format = db.Column(db.String(200), nullable=True) create_date = db.Column(db.DateTime(), nullable=False) modify_date = db.Column(db.DateTime(), nullable=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), nullable=False) user = db.relationship('User', backref=db.backref('apireq_set'))
class Apires(db.Model): #기본키 id = db.Column(db.Integer, primary_key=True) apireq_id = db.Column(db.Integer, db.ForeignKey('apireq.id', ondelete='CASCADE')) apireq = db.relationship('Apireq', backref=db.backref('apires_set')) parameter = db.Column(db.String(200), nullable=False) result_format = db.Column(db.String(200), nullable=True) result_data = db.Column(db.Text(), nullable=False) create_date = db.Column(db.DateTime(), nullable=False) modify_date = db.Column(db.DateTime(), nullable=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), nullable=False) user = db.relationship('User', backref=db.backref('apires_set'))
class Answer(db.Model): id = db.Column(db.Integer, primary_key=True) question_id = db.Column(db.Integer, db.ForeignKey("question.id", ondelete="CASCADE")) question = db.relationship("Question", backref=db.backref("answer_set")) content = db.Column(db.Text(), nullable=False) create_date = db.Column(db.DateTime(), nullable=False) modify_date = db.Column(db.DateTime(), nullable=True) user_id = db.Column(db.Integer, db.ForeignKey("user.id", ondelete="CASCADE"), nullable=False) user = db.relationship("User", backref=db.backref("answer_set")) voter = db.relationship("User", secondary=answer_voter, backref=db.backref("answer_voter_set"))
class Question(db.Model): #질문을 등록할 수 있는 테이블 생성 id = db.Column(db.Integer, primary_key=True) #이 테이블에 id라는 컬럼을 만들고, int형 데이터 넣어라. subject = db.Column(db.String(200), nullable=False) #200글자로 제한. nullable= 비어두기 허용 유무.. content = db.Column(db.Text(), nullable=False) create_date = db.Column(db.DateTime(), nullable=False)
class Question(db.Model ): #SQLAlchemy의 Model을 인자로 받음. DB에서 Question이라는 테이블을 생성하는 것과 동일 id = db.Column(db.Integer, primary_key=True) #이 변수들을 각각 Question 테이블의 컬럼이 됨 subject = db.Column(db.String(200), nullable=False) #여기서 200은 사이즈 제한 content = db.Column(db.Text(), nullable=False) # String과 달리 Text는 글자 수 제한이 없음 create_date = db.Column(db.DateTime(), nullable=False)
class Question(db.Model): # 플라스크는 데이터 타입이 db.Interger 이고 기본키로 지정한 속성은 # 값이 자동으로 증가하는 특징이 있어서 데이터를 저장할 떄 해당 속성값을 지정하지 않아도 # 1씩 자동으로 증가하여 저장된다. id = db.Column(db.Integer, primary_key=True) subject = db.Column(db.String(200), nullable=False) content = db.Column(db.Text(), nullable=False) create_date = db.Column(db.DateTime(), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), nullable=False) user = db.relationship('User', backref=db.backref( 'question_set')) # Question 모델에서 User 모델을 참조하기 위한 필드 modify_date = db.Column(db.DateTime(), nullable=True) voter = db.relationship('User', secondary=question_voter, backref=db.backref('question_vote_set'))
class Comment(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), nullable=False) user = db.relationship('User', backref=db.backref('comment_set')) content = db.Column(db.Text(), nullable=False) create_date = db.Column(db.DateTime(), nullable=False) modify_date = db.Column(db.DateTime()) # 댓글은 질문 혹은 답변 둘 중 하나에 달 수 있으므로 널이 가능해야 한다 question_id = db.Column(db.Integer, db.ForeignKey('question.id', ondelete='CASCADE'), nullable=True) question = db.relationship('Question', backref=db.backref('comment_set')) answer_id = db.Column(db.Integer, db.ForeignKey('answer.id', ondelete='CASCADE'), nullable=True) answer = db.relationship('Answer', backref=db.backref('comment_set'))
class Answer(db.Model): id = db.Column(db.Integer, primary_key=True) question_id = db.Column(db.Integer, db.ForeignKey('question.id', ondelete='CASCADE')) question = db.relationship('Question', backref=db.backref( 'answer_set', cascade='all, delete-orphan')) content = db.Column(db.Text(), nullable=False) create_date = db.Column(db.DateTime(), nullable=False)
class Answer(db.Model): id = db.Column(db.Integer, primary_key=True) question_id = db.Column( db.Integer, db.ForeignKey('question.id', ondelete='CASCADE') ) #db.ForeignKey에 지정한 첫 번째 값은 연결한 모델의 속성명이고 두 번째 ondelete에 지정한 값은 삭제 연동 설정이다. 즉, 답변 모델의 quesion_id 속성은 질문 모델의 id 속성과 연결되며 ondelete='CASCADE'에 의해 데이터베이스에서 쿼리를 이용하여 질문을 삭제하면 해당 질문에 달린 답변도 함꼐 삭제된다. question = db.relationship( 'Question', backref=db.backref('answer_set') ) #quesion속성은 답변 모델에서 질문 모델을 참조하기 위해 추가했다. 예를 들어 답변 모델 객체에서 질문 모델 객체의 제목을 참조하려면 answer.question.subject처럼 할 수 있다. 이렇게 하려면 속성을 추가할 때 db.Column이 아닌 db.relationship을 사용해야한다. content = db.Column(db.Text(), nullable=False) create_date = db.Column(db.DateTime(), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), nullable=False) user = db.relationship('User', backref=db.backref('answer_set')) modify_date = db.Column(db.DateTime(), nullable=True) voter = db.relationship('User', secondary=answer_voter, backref=db.backref('answer_voter_set'))
class User(db.Model): user_id = db.Column(db.Integer, primary_key=True) pw = db.Column(db.String(20), nullable=False) name = db.Column(db.String(10), nullable=False) age = db.Column(db.Integer, nullable=False) address = db.Column(db.String(40), nullable=False) birth = db.Column(db.String(20), nullable=False) gender = db.Column(db.String(2), nullable=False) create_date = db.Column(db.DateTime(), nullable=False)
class Answer(db.Model): id = db.Column(db.Integer, primary_key=True) #primary_key는 고윳값. 주민등록같은거 question_id = db.Column( db.Integer, db.ForeignKey('question.id', ondelete='CASCADE') ) #ForeignKey 다른 테이블의 pk(primary key)값을 쓸때 여기서는 foreignkey라고 함. ondelete= 원래 것이 삭제디면 여기서도 삭제되라. 연동의 의미.. question = db.relationship('Question', backref=db.backref( 'answer_set')) #참조 #backref는 역참조. relationship은 묶어주는 역할 content = db.Column(db.Text(), nullable=False) create_date = db.Column(db.DateTime(), nullable=False)
class Answer(db.Model): id = db.Column(db.Integer, primary_key=True) question_id = db.Column(db.Integer, db.ForeignKey('question.id', ondelete='CASCADE')) question = db.relationship('Question', backref=db.backref('answer_set', )) # question = db.relationship('Question', \ # backref=db.backref('answer_set', \ # cascade='all, delete-orphan' )) # # 이렇게 하면 질문과 연관된 답변 데이터 전부 삭제 content = db.Column(db.Text(), nullable=False) create_date = db.Column(db.DateTime(), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), nullable=False) user = db.relationship('User', backref=db.backref('answer_set')) modify_date = db.Column(db.DateTime(), nullable=True) voter = db.relationship('User', secondary=answer_voter, backref=db.backref('answer_voter_set'))
class Question(db.Model): id = db.Column(db.Integer, primary_key=True) subject = db.Column(db.String(200), nullable=False) content = db.Column(db.Text(), nullable=False) create_date = db.Column(db.DateTime(), nullable=False) modify_date = db.Column(db.DateTime(), nullable=True) user_id = db.Column( db.Integer, db.ForeignKey("user.id", ondelete="CASCADE"), nullable=False, # server_default="1", ) # User 스키마의 id를 참조함 user = db.relationship( "User", backref=db.backref("question_set")) # question을 이용해 user를 참조하기 위함 voter = db.relationship("User", secondary=question_voter, backref=db.backref("question_voter_set"))
class Answer(db.Model): id = db.Column(db.Integer, primary_key=True) question_id = db.Column(db.Integer, db.ForeignKey("question.id", ondelete="CASCADE")) question = db.relationship( "Question", backref=db.backref("answer_set") ) # backref 속성은 answer.question.subject 와는 반대로 질문에서 답변모델을 참조하기 위해서 사용되는 속성이다. 중요한 기능 content = db.Column(db.Text(), nullable=False) create_date = db.Column(db.DateTime(), nullable=False) modify_date = db.Column(db.DateTime(), nullable=True) user_id = db.Column(db.Integer, db.ForeignKey("user.id", ondelete="CASCADE"), nullable=False # server_default="1", ) user = db.relationship("User", backref=db.backref("answer_set")) voter = db.relationship("User", secondary=answer_voter, backref=db.backref("answer_voter_set"))
class Answer(db.Model): id = db.Column(db.Integer, primary_key=True) question_id = db.Column(db.Integer, db.ForeignKey('question.id', ondelete='CASCADE') ) #외래키, Question의 PK값(id)을 사용, 질문 삭제되면 같이 삭제되도록 설정 question = db.relationship( 'Question', backref=db.backref('answer_set') ) #컬럼이 아니고 관계(역참조 - question_id가 똑같은 답변들은 해당 id의 질문을 참조하여 하나로 묶어줌) #즉, 어떤 질문에 대한 답변인지 나타내기 위한 것 content = db.Column(db.Text(), nullable=False) create_date = db.Column(db.DateTime(), nullable=False)
class Answer(db.Model): id = db.Column(db.Integer, primary_key=True) question_id = db.Column( db.Integer, db.ForeignKey('question.id', ondelete='CASCADE')) # 질문 삭제 시 답변도 같이 삭제됨됨 question = db.relationship( 'Question', backref=db.backref('answer_set')) # db.relationship(참조할 모델, 역참조 설정) # 역참조 설정을 해두면 Answer 객체에서 일방적으로 Question 객체를 참조하는 경우라도 그 반대의 참조도 가능해진다(Question 객체에서 Answer 객체 참조 가능) # answer_set : 역참조 할 경우 사용하는 키워드 content = db.Column(db.Text(), nullable=False) create_date = db.Column(db.DateTime(), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), nullable=False) user = db.relationship('User', backref=db.backref('answer_set')) modify_date = db.Column(db.DateTime(), nullable=True) voter = db.relationship('User', secondary=answer_voter, backref=db.backref('answer_voter_set'))
class user_info(db.Model): id = db.Column(db.Integer, primary_key=True) pw = db.Column(db.String(10), nullable=False) name = db.Column(db.String(5), nullable=False) phone = db.Column(db.String(15), nullable=False) age = db.Column(db.String(2), nullable=False) birth = db.Column(db.String(8), nullable=False) gender = db.Column(db.String(2), nullable=False) subject = db.Column(db.String(200), nullable=False) content = db.Column(db.Text(), nullable=False) create_date = db.Column(db.DateTime(), nullable=False)
class Question(db.Model): #기본키 id = db.Column(db.Integer, primary_key=True) subject = db.Column(db.String(200), nullable=False) content = db.Column(db.Text(), nullable=False) create_date = db.Column(db.DateTime(), nullable=False) modify_date = db.Column(db.DateTime(), nullable=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), nullable=False) user = db.relationship('User', backref=db.backref( 'question_set')) #backref : User 모델 데이터를 통해 Question 모델 데이터를 참조 voter = db.relationship('User', secondary=question_voter, backref=db.backref('question_voter_set')) menu_id = db.Column(db.Integer(), db.ForeignKey('menu.id', ondelete='CASCADE'), nullable=False, server_default='1') view_cnt = db.Column(db.Integer, nullable=False, server_default='0')
class Userinfo(db.Model): user_id = db.Column(db.Text(), primary_key=True) #user_pw= db.Column(db.Integer, db.ForeignKey('user_name',ondelete='CASCADE')) #user_name = db.relationship('Question',backref=db.backref('answer_set')) user_pw = db.Column(db.Integer(), nullable=False) user_name = db.Column(db.Text(), nullable=False) user_age = db.Column(db.Integer(), nullable=False) user_add = db.Column(db.Text(), nullable=False) user_sex = db.Column(db.Text(), nullable=False) user_sch = db.Column(db.Text(), nullable=False) user_hby = db.Column(db.Text(), nullable=False) user_date = db.Column(db.Integer(), nullable=False) create_date = db.Column(db.DateTime(), nullable=False)
class Question(db.Model): id = db.Column(db.Integer, primary_key=True) subject = db.Column(db.String(200), nullable=False) content = db.Column(db.Text(), nullable=False) create_date = db.Column(db.DateTime(), nullable=False) # db.ForeignKey('user.id', ondelete='CASCADE') # User 모델의 id 값과 연결 # ondelete='CASCADE' user 데이터 삭제 시에 Question 모델 데이터도 함께 삭제되도록함 user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), nullable=False) user = db.relationship('User', backref=db.backref('question_set')) # 수정일자 컬럼 modify_date = db.Column(db.DateTime(), nullable=True) # 추천인 # secondary: voter가 다대다 관계이며, question_voter 테이블을 참조한다. voter = db.relationship('User', secondary=question_voter, backref=db.backref('question_voter_set'))