簡體   English   中英

Python Flask,SQLAlchemy關系

[英]Python Flask, SQLAlchemy relationship

我已經嘗試解決這個問題幾個小時了,我無法讓SQLAlchemy工作(它正在工作,直到我把兩個新功能,用戶和注冊)

from flask.ext.sqlalchemy import SQLAlchemy
from . import app
from datetime import datetime

db = SQLAlchemy(app)

class PasteCode(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    codetitle = db.Column(db.String(60), unique = True)
    codebody = db.Column(db.Text)
    pub_date = db.Column(db.DateTime)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    parent_id = db.Column(db.Integer, db.ForeignKey('paste_code.id'))
    parent = db.relationship('PasteCode', lazy = True, backref = 'children', uselist = False, remote_side = [id])

    def __init__(self, codetitle, codebody, parent = None):
        self.codetitle = codetitle
        self.codebody = codebody
        self.pub_date = datetime.utcnow()
        self.parent = parent

class User(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    display_name = db.Column(db.String(30))
    #pastes = db.Column(db.Integer, db.ForeignKey('paste_code.id'))
    pastes = db.relationship(PasteCode, lazy = "dynamic", backref = "user")

class Registration(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    username = db.Column(db.String(30), unique = True)
    password = db.Column(db.String(100), unique = False)

這是它在運行時給我的回溯:

OperationalError: (OperationalError) no such table: paste_code u'SELECT paste_code.id AS paste_code_id, paste_code.codetitle AS paste_code_codetitle, paste_code.codebody AS paste_code_codebody, paste_code.pub_date AS paste_code_pub_date, paste_code.user_id AS paste_code_user_id, paste_code.parent_id AS paste_code_parent_id \nFROM paste_code' ()

我也試過這個:

from flask.ext.sqlalchemy import SQLAlchemy
from . import app
from datetime import datetime

db = SQLAlchemy(app)

class PasteCode(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    codetitle = db.Column(db.String(60), unique = True)
    codebody = db.Column(db.Text)
    pub_date = db.Column(db.DateTime)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    parent_id = db.Column(db.Integer, db.ForeignKey('pastecode.id'))
    parent = db.relationship('PasteCode', lazy = True, backref = 'children', uselist = False, remote_side = [id])

    def __init__(self, codetitle, codebody, parent = None):
        self.codetitle = codetitle
        self.codebody = codebody
        self.pub_date = datetime.utcnow()
        self.parent = parent

class User(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    display_name = db.Column(db.String(30))
    pastes =  db.relationship(PasteCode, lazy = 'dynamic', backref = 'user')
    #pastes = db.relationship(PasteCode, lazy = "dynamic", backref = "user")

class Registration(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    username = db.Column(db.String(30), unique = True)
    password = db.Column(db.String(100), unique = False)

我收到了這個錯誤:

ArgumentError: Could not determine join condition between parent/child tables on relationship PasteCode.parent. Specify a 'primaryjoin' expression. If 'secondary' is present, 'secondaryjoin' is needed as well.

任何的想法? 謝謝!

我補充說,我修復它的方式很簡單

__tablename__ = "paste_code"

並且一切正常,我認為SQLAlchemy沒有正確檢查表名。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM