【问题标题】:Why am I getting a "cross-database references are not implemented"?为什么我会收到“未实现跨数据库引用”?
【发布时间】:2021-08-25 12:33:28
【问题描述】:

对 postgresql 数据库的非常简单的更新,但它不起作用。 sql select 语句很好,并返回正确的值。

当我进行更新时,它会抛出错误:

 {"ERROR [0A000] ERROR: cross-database references are not implemented: "openerp.public.product_template"; Error while executing the query"}.

我正在使用 vb.net 和 postgresql 9.2。

我想要做的只是更改名称字段以匹配描述中的内容。

log:
LOG 0   duration: 34.000 ms  statement: SELECT * FROM product_template where import_date = '08/22/2013'
LOG 0   duration: 11.000 ms  statement: select n.nspname, c.relname, a.attname, a.atttypid, t.typname, a.attnum, a.attlen, a.atttypmod, a.attnotnull, c.relhasrules, c.relkind, c.oid, d.adsrc from (((pg_catalog.pg_class c inner join pg_catalog.pg_namespace n on n.oid = c.relnamespace and c.oid = 20496) inner join pg_catalog.pg_attribute a on (not a.attisdropped) and a.attnum > 0 and a.attrelid = c.oid) inner join pg_catalog.pg_type t on t.oid = a.atttypid) left outer join pg_attrdef d on a.atthasdef and d.adrelid = a.attrelid and d.adnum = a.attnum order by n.nspname, c.relname, attnum
LOG 0   duration: 12.000 ms  parse _PLAN000000001D2CFB60: SELECT * FROM product_template where import_date = '08/22/2013'
LOG 0   duration: 11.000 ms  statement: select ta.attname, ia.attnum, ic.relname, n.nspname, tc.relname from pg_catalog.pg_attribute ta, pg_catalog.pg_attribute ia, pg_catalog.pg_class tc, pg_catalog.pg_index i, pg_catalog.pg_namespace n, pg_catalog.pg_class ic where tc.oid = 20496 AND tc.oid = i.indrelid AND n.oid = tc.relnamespace AND i.indisprimary = 't' AND ia.attrelid = i.indexrelid AND ta.attrelid = i.indrelid AND ta.attnum = i.indkey[ia.attnum-1] AND (NOT ta.attisdropped) AND (NOT ia.attisdropped) AND ic.oid = i.indexrelid order by ia.attnum
LOG 0   duration: 0.000 ms  statement: select current_schema()
LOG 0   duration: 1.000 ms  statement: select c.relhasrules, c.relkind, c.relhasoids from pg_catalog.pg_namespace u, pg_catalog.pg_class c where u.oid = c.relnamespace and c.relname = 'product_template' and u.nspname = 'public'
LOG 0   duration: 1.000 ms  statement: select c.relhasrules, c.relkind, c.relhasoids from pg_catalog.pg_namespace u, pg_catalog.pg_class c where u.oid = c.relnamespace and c.relname = 'product_template' and u.nspname = 'public'
ERROR   0A000   cross-database references are not implemented: "openerp.public.product_template"

代码:

Private Sub btnChgNameToDescr_Click(sender As Object, e As EventArgs) Handles btnChgNameToDescr.Click

    Dim objConn As New System.Data.Odbc.OdbcConnection
    Dim objCmd As New System.Data.Odbc.OdbcCommand
    Dim dtAdapter As New System.Data.Odbc.OdbcDataAdapter
    Dim ds As New DataSet

    Me.Cursor = System.Windows.Forms.Cursors.WaitCursor

    Dim strConnString As String
    Dim strSQL As String
    Dim iRecCount As Integer

    Me.Cursor = System.Windows.Forms.Cursors.WaitCursor

    If objConn.State = ConnectionState.Open Then
        'do nothing
    Else
        strConnString = "Dsn=PostgreSQL35W;database=OpenERP;server=localhost;port=5432;uid=openpg;pwd=openpgpwd"
        objConn.ConnectionString = strConnString
        objConn.Open()
    End If


    If Me.txtImportDate.Text = "" Then
        MsgBox("Import Date field cannot be blank.")
        Exit Sub
    End If

    Dim str_import_date As String = Me.txtImportDate.Text


    strSQL = "SELECT * FROM product_template where import_date = " & "'" & str_import_date & "'"

    dtAdapter.SelectCommand = objCmd

    With objCmd
        .Connection = objConn
        .CommandText = strSQL
        .CommandType = CommandType.Text
        .ExecuteNonQuery()

        dtAdapter.Fill(ds, "product_template")

        iRecCount = ds.Tables("product_template").Rows.Count

    End With

    If iRecCount = 0 Then
        MsgBox("No records found.")
        Me.Cursor = System.Windows.Forms.Cursors.Default
        Exit Sub
    End If


    Dim cb As New Odbc.OdbcCommandBuilder(dtAdapter)


    'change the name field to item_description
    With ds
        For i As Integer = 0 To .Tables("product_template").Rows.Count - 1

            'this works, returns a string
            Dim str_default_code As String = (.Tables(0).Rows(i).Item("name").ToString)
            'this works
            Dim str_item_description As String = (.Tables(0).Rows(i).Item("description").ToString)

            .Tables("product_template").Rows(i).Item("name") = str_item_description
            'setting the variable doesn't work either - Dim str_item_description As String = "BH LITE BRT"

            'this throws the error
            dtAdapter.Update(ds, "product_template")

        Next
    End With

    Me.Cursor = System.Windows.Forms.Cursors.Default
End Sub

【问题讨论】:

  • 我不知道这是否是您的问题,但 SELECT 查询不需要代码中间的 ExecuteNonQuery()
  • 通常表示带有多个点的未引用标识符,Postgres 会将其解释为db.schema.table,从而触发所述异常。我会通过查看 db 服务器日志文件 立即找到原因,在该文件中记录了实际的违规语句以及带有默认日志记录设置的错误消息。
  • @ErwinBrandstetter 就像 str_import_date = 22.08.2013。字符串连接总会找到方法来踢你
  • steve - executenonquery 是一个错误,但不是问题 erwin - 我同意,但 select 语句工作正常,所以我不认为是这样......我会测试一下。我编辑了我的帖子以包含日志文件摘录...我不明白...
  • 好的,将 sql 更改为 strSQL = "SELECT * FROM product_template where id = '180088'",不走运,得到同样的错误......但仍然得到变量的返回值。跨度>

标签: vb.net postgresql


【解决方案1】:

如果您在 DBeaver 中遇到此错误,将目标数据库设置为活动可能会解决它:

这与multi-database support有关

【讨论】:

    【解决方案2】:

    在您的 postgresql 日志中查找错误以查看实际发送到数据库的内容。我不知道如何修复您的代码,因为我不太了解该平台。此外,您需要转向参数化查询,因为您当前的方法将受到 sql 注入问题的影响。

    但是,您的错误意味着您有一个额外的命名空间。正常的命名空间是schema.table.columnschema.table,具体取决于上下文。如果您尝试将表指定为schema.table.column,它将读取为database.schema.table 并抛出此错误。同样,如果您有一个额外的点,您可能会意外指定 database.schema.table.column(这也会引发错误)。

    这就是为什么字符串化 SQL 确实是个坏主意的部分原因,但并没有真正触及问题的表面。

    【讨论】:

    • 是的,这对我有用。我有缺陷的命名空间是“postgres.public.tsola_team”,现在是“public.tsola_team”。
    【解决方案3】:

    我在使用 DataGrip(2020.3 版)时遇到了同样的问题。

    发生这种情况是因为我更改了连接设置,但仍然使用相同的控制台窗口。

    我找到了两种解决方法:

    1. 更改控制台的会话(在控制台右键-->切换会话-->新建会话)
    2. 打开一个新的控制台窗口

    【讨论】:

      【解决方案4】:

      今天正在做某事并得到了这个 - 最后是因为我的数据库名称中有大写字母,而命令没有 - prodCRT.public.tablename vs. prodcrt.public.tablename

      只要我将架构重命名为小写,我之前工作的所有命令都会再次工作。

      注意:原来的数据库只被称为“prod”——所以当我把它叫做 prodCRT 时事情就坏了

      【讨论】:

        【解决方案5】:

        在对带有前缀架构名称的表进行截断时,发现 Redshift 出现同样的错误。

        截断 schema_name.table_name;

        虽然从 S3 加载复制是在没有架构名称的情况下完成的。 总而言之,Truncate table 应该与 Copy 命令中使用的模式相同,以避免在 Redshift 中出现此错误。

        https://docs.aws.amazon.com/redshift/latest/dg/r_TRUNCATE.html

        【讨论】:

          猜你喜欢
          • 2019-01-17
          • 2019-01-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-23
          相关资源
          最近更新 更多