为什么我得到一个"关系不存在" sqlalchemy元数据的现有表的错误?

时间:2018-04-21 05:43:34

标签: python postgresql sqlalchemy

我有以下代码会引发以下错误

engine = create_engine('postgresql+psycopg2:....', convert_unicode=True)

metadata = sqlalchemy.MetaData()
table = sqlalchemy.Table('omni.all_order', metadata,
    sqlalchemy.Column('o_id', sqlalchemy.Integer),
    sqlalchemy.Column('order', sqlalchemy.String),

)

ins = table.insert().values(all_rows)
engine.execute(ins)
  

sqlalchemy.exc.ProgrammingError:(psycopg2.ProgrammingError)关系   " omni.all_order"不存在

但以下两个代码工作正常

engine = create_engine('postgresql+psycopg2:....', convert_unicode=True)

    result = engine.execute("SELECT * from omni.all_order ")
    rows = result.fetchall()
    print(rows)

-

 engine = create_engine('postgresql+psycopg2:....', convert_unicode=True)

    engine.execute("INSERT INTO omni.all_order (o_id) VALUES (1) ")

首先在同一模式(omni)中创建另一个表会引发相同的错误

engine = create_engine('postgresql+psycopg2:....', convert_unicode=True)

    result = engine.execute("CREATE TABLE omni.all_order_s(o_id INT, order VARCHAR(80))")

    metadata = sqlalchemy.MetaData()
    table = sqlalchemy.Table('omni.all_order_s', metadata,
        sqlalchemy.Column('o_id', sqlalchemy.Integer),
        sqlalchemy.Column('order', sqlalchemy.String),

    )

    ins = table.insert().values(all_rows)
    engine.execute(ins)
  

sqlalchemy.exc.ProgrammingError:(psycopg2.ProgrammingError)关系   " omni.all_order_s"不存在

但在架构之外创建它可以正常工作

engine = create_engine('postgresql+psycopg2:....', convert_unicode=True)

    result = engine.execute("CREATE TABLE all_order_s(o_id INT, order VARCHAR(80))")

        metadata = sqlalchemy.MetaData()
        table = sqlalchemy.Table('all_order_s', metadata,
            sqlalchemy.Column('o_id', sqlalchemy.Integer),
            sqlalchemy.Column('order', sqlalchemy.String),

        )

        ins = table.insert().values(all_rows)
        engine.execute(ins)

为什么会这样?

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,并且在以下链接中找到了解决方案:https://dba.stackexchange.com/questions/192897/postgres-relation-does-not-exist-error

当您通过变量创建表名时,该名称将带引号传递,因此该名称区分大小写,并且在再次调用时需要带引号。

答案 1 :(得分:0)

使用schema=关键字参数传递表格的架构,而不是将其包含在表格的名称中:

df2 %>% left_join(df1) %>% select(-START_WEEK, -n, -min_START_WEEK) %>%
  group_by(Prod_ID) %>%
  arrange(Prod_ID, week) %>%
  mutate(tot_market = zoo::na.locf(tot_market)) %>%
  spread(week, tot_market) %>%
  ungroup() %>%
  mutate_at(vars(Prod_ID), as.character) %>%
  rename_if(is.integer, function(x) paste0("Week", x))

# # A tibble: 4 x 193
#   Prod_ID     Week1 Week2 Week3 Week4 Week5 Week6 Week7 Week8 Week9 Week10 Week11
#   <chr>       <int> <int> <int> <int> <int> <int> <int> <int> <int>  <int>  <int>
# 1 11044913000     1     1     1     1     1     1     1     1     1      1      1
# 2 11159402003     2     2     2     2     2     2     2     3     3      3      3
# 3 11159410010     2     3     3     3     4     4     4     4     4      4      4
# 4 11159410014     1     1     1     1     1     1     1     1     1      1      1
# # ... with 181 more variables

目前它被引用为整体。