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) # user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), nullable=True, server_default='1') 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 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)
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.ForeignKaey는 다른 모델과의 연결 의미 ,ondelete=cascade는 이 답변과 연결된 질문이 삭제될시 답변도 삭제 question = db.relationship('Question', backref=db.backref('answer_set')) #backref속성은 질문에서 답변모델을 참조하기위해서 사용 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=True) 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) 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'))
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)
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 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 VocaWord(db.Model): # 단어 id = db.Column(db.Integer, primary_key=True) voca_id = db.Column(db.Integer, db.ForeignKey('voca.id', ondelete="CASCADE")) voca = db.relationship('Voca', backref=db.backref('voca_set', cascade='all, delete-orphan')) word = db.Column(db.String(200), nullable=False) # 단어장 단어 mean = db.Column(db.String(200), 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') ) #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 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 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 Voca(db.Model): # 단어장 id = db.Column(db.Integer, primary_key=True) # 단어장 고유번호 vocaname = db.Column(db.String(150), nullable=False) # 단어장 이름 user_id = db.Column(db.String(150), db.ForeignKey('user.id', ondelete="CASCADE")) user = db.relationship( 'User', backref=db.backref('voca_set', cascade='all, delete-orphan')) # 유저 데이터 RK & 역참조 tag = db.Column(db.Text(), 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 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 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 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 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) # 글자수 제한없음 Text create_date = db.Column(db.DateTime(), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), nullable=False) # server_default='1') 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 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 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 Answer(db.Model): id = db.Column(db.Integer, primary_key = True) # 질문 모델과 연결. db.ForeignKey(연결할 기존 모델의 속성, 삭제 연동 설정) - 질문을 삭제하면 해당 질문에 달린 답변도 함께 삭제된다. question_id = db.Column(db.Integer, db.ForeignKey('question.id', ondelete='CASCADE')) """ 답변 모델에서 질문 모델을 참조. db.relationship(참조할 모델, 역참조 설정 - 질문에서도 답변을 참조하도록..?) 만약 질문 데이터를 삭제할 때 연관된 답변 데이터가 모두 삭제되기를 바란다면, db.backref 설정에 cascade='all, delete-orphan' 옵션을 추가해야 한다. """ 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 = 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) #p.212 voter = db.relationship('User', secondary=answer_voter, backref=db.backref('answer_voter_set'))
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 Answer(db.Model): id = db.Column(db.integer, primary_key=True)# 이 역시 id는 고유번호이기 때문에 기본키로 설정 question_id = db.Column(db.integer, db.ForeignKey('question_id', ondelete='CASCADE'))# 질문모델과 연결하기 위해 사용 어떤 속성을 기존 모델과 연결하려면 foreignkey를 사용해야함 # db.ForeignKey(연결할 기존 모델 속성, 삭제 연동 설정) #삭제 연동 설정이란 질문 삭제시 question_id 도 삭제됨을 의미 content = db.Column(db.Text(), nullable=False) create_date=db.Column(db.DateTime(), nullable=False) ''' question = db.relationship('Question', backref=db.backref('answer_set')) 기존 모델 참조 참조 모델 backref는 역참조 설정 즉 질문에서 답변을 참조 질문에 해당하는 객체가 a_question이면 a_question.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) 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 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'))
class Question(db.Model): # 모든 모델의 기본 클래스인 db.Model 상속받음. # 질문 모델 생성. 속성 = db.Column(데이터 타입, ~) # primary_key = 기본 키. nullable = 속성에 빈 값을 허용할 것인지. 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) # secondary 설정으로 voter는 N:N 관계이며 question_voter 테이블 참조함을 알려줌 # 어떤 계정이 a_user라는 객체로 참조되면 backref에 의해 a_user.question_voter_set으로 질문 구할 수 있게 됨 # backref 설정에 사용하는 이름은 중복 불가 voter = db.relationship('User', secondary=question_voter, backref=db.backref('question_voter_set')) # question model에 user_id, user field 추가 # user_id : User model data의 id값을 Question model에 포함시키기 위함 user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), nullable=False) # user : Question model에서 User model을 참조하기 위한 field # backref로 User model data 통해 Question model data 참조 user = db.relationship('User', backref=db.backref('question_set'))
from pybo import db question_voter = db.Table( 'question_voter', db.Column('user_id', db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), primary_key=True), db.Column( 'question_id', db.Integer, db.ForeignKey('question.id', ondelete='CASCADE', primary_key=True))) answer_voter = db.Table( 'answer_voter', db.Column('user_id', db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), primary_key=True), db.Column('answer_id', db.Integer, db.ForeignKey('answer.id', ondelete='CASCADE'), primary_key=True)) 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) user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'),
from pybo import db question_voter = db.Table( 'question_voter', db.Column('user_id', db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), primary_key=True), db.Column('question_id', db.Integer, db.ForeignKey('question.id', ondelete='CASCADE'), primary_key=True)) 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) 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) voter = db.relationship('User', secondary=question_voter, backref=db.backref('question_voter_set')) answer_voter = db.Table( 'answer_voter',
from pybo import db question_voter = db.Table( 'question_voter', db.Column('user_id', db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), primary_key=True), db.Column('question_id', db.Integer, db.ForeignKey('question.id', ondelete='CASCADE'), primary_key=True) ) 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')) voter = db.relationship('User', secondary=question_voter, backref=db.backref('question_voter_set')) answer_voter = db.Table( 'answer_voter', db.Column('user_id', db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), primary_key=True), db.Column('answer_id', db.Integer, db.ForeignKey('answer.id', ondelete='CASCADE'), primary_key=True) ) class Answer(db.Model): id = db.Column(db.Integer, primary_key=True) question_id = db.Column(db.Integer, db.ForeignKey('question.id', ondelete='CASCADE'))