簡體   English   中英

執行SQL查詢時無法引用表中的列:列不存在

[英]Column in table cannot be referenced when executing SQL query: column does not exist

當嘗試使用下面的代碼在我的數據庫中插入測試數據時,我收到此錯誤。 我知道我的數據庫設置正確並且可以與我的 Flask 應用程序一起使用,但我無法使用我的MOCK_DATA.csv文件中的值為數據庫播種

sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedColumn) column "content" does not exist
LINE 3:         (content, rating, book_id, (SELECT user_id FROM user...
                 ^
HINT:  There is a column named "content" in table "reviews", but it cannot be referenced from this part of the query.

[SQL: --sql
        INSERT INTO reviews VALUES
        (content, rating, book_id, (SELECT user_id FROM users WHERE username=%(username)s))
        --endsql]
[parameters: {'username': 'Brent'}]

架構:評論表

CREATE TABLE IF NOT EXISTS reviews (
    review_id SERIAL PRIMARY KEY,
    content TEXT,
    rating INTEGER,
    user_id INTEGER REFERENCES users,
    book_id INTEGER REFERENCES books

架構:用戶表

CREATE TABLE IF NOT EXISTS users (
    user_id SERIAL PRIMARY KEY,
    username VARCHAR(255),
    password VARCHAR(255)

種子.py

FILE = open("MOCK_DATA.csv")
reader = csv.reader(FILE)

firstline = True
for username, password, content, rating in reader:
    # skip first line of csv file
    if firstline:
        firstline = False
        continue

    # fill users table
    DB.execute(
        """--sql
    INSERT INTO users (username, password) VALUES 
    (:username, :password) ON CONFLICT DO NOTHING
    --endsql""", {
            "username": username,
            "password": generate_password_hash(password)
        })

    print("added user")

    # add review for user
    DB.execute(
        """--sql
    INSERT INTO reviews VALUES
    (content, rating, book_id, 
    (SELECT user_id FROM users WHERE username=:username))
    --endsql""", {
            "content": content,
            "rating": rating,
            "book_id": 288,
            "username": username,
        })

    print("added review")
    DB.commit()

MOCK_DATA.csv

username,password,content,rating
Brent,OoDQgc,"Sed vel enim sit amet nunc viverra dapibus. Nulla suscipit ligula in lacus. Curabitur at ipsum ac tellus semper interdum. Mauris ullamcorper purus sit amet nulla. Quisque arcu libero, rutrum ac, lobortis vel, dapibus at, diam.",1

您不能在 VALUES 子句中引用目標表的列名。

嘗試在 SQL 中改為:

INSERT INTO reviews (content, rating, book_id) VALUES
    (v_content, v_rating, v_book_id,
    (SELECT user_id FROM users WHERE username=:username))

其中 v_xxx 變量在 Python 中定義。

我嘗試在 DBeaver 中運行單個查詢並發現我插入的值錯誤,所以 VALUES 應該是(:content,:rating,:book_id 等)

用戶名也不是唯一的,因此我必須將LIMIT 1添加到usernameSELECT語句

FILE = open("MOCK_DATA.csv")
reader = csv.reader(FILE)

firstline = True
for username, password, content, rating in reader:
    # skip first line of csv file
    if firstline:
        firstline = False
        continue

    # fill users table
    DB.execute(
        """--sql
    INSERT INTO users (username, password) VALUES 
    (:username, :password) ON CONFLICT DO NOTHING
    --endsql""", {
            "username": username,
            "password": generate_password_hash(password)
        })

    print("added user")

    # add review for user
    DB.execute(
        """--sql
    INSERT INTO reviews (content, rating, book_id, user_id) VALUES
    (:content, :rating, :book_id, 
    (SELECT user_id FROM users WHERE username=:username LIMIT 1))
    --endsql""",
        {
            "content": content,
            "rating": rating,
            # add book_id for book you want to add the reviews for
            "book_id": 288,
            "username": username
        })

    print("added review")
    DB.commit()

我的情況也有同樣的錯誤。 我的查詢是這樣的:

insert into myTable
   select
   (SELECT COALESCE (Max(idTable) + 1, 1) ),
   999,
   'foo'

通過添加“從表”修復,即:

insert into myTable
   select
   (SELECT COALESCE (Max(idTable) + 1, 1) from myTable),
   999,
   'foo'

暫無
暫無

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

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