简体   繁体   English

使用SQLAlchemy的Oracle数据库架构

[英]Oracle database schema using SQLAlchemy

I have a problem with SqlAlchemy. 我有SqlAlchemy问题。 When I define the schema in my Oracle database, foreign keys are not recognized in other tables. 当我在Oracle数据库中定义架构时,在其他表中无法识别外键。 Tested without using layout and it worked. 在不使用布局的情况下进行了测试,并且有效。 But I need to define the schema of this application because the user is not the owner of the tables. 但是我需要定义此应用程序的架构,因为用户不是表的所有者。 I would like your help to solve this problem. 我希望您能帮助您解决这个问题。 code: 码:

Base = declarative_base()
SCHEMA = {'schema' : 'SIATDESV'}

class Pgdasd(Base):
    __tablename__ = 'PGDASD'
    __table_args__ = SCHEMA

    PGDASD_00000_ID_DECLARACAO    = Column(String(17), primary_key = True)

class Pgdasd_01000(Base):
    __tablename__ = 'pgdasd_01000'
    __table_args__ = SCHEMA

    PGDASD_00000_ID_DECLARACAO  = Column(String, ForeignKey('PGDASD.PGDASD_00000_ID_DECLARACAO'))   
    PGDASD_01000_NRPAGTO        = Column(String, primary_key = True)

error: *Foreign key associated with column 'pgdasd_01000.PGDASD_00000_ID_DECLARACAO' could not find table 'PGDASD' with which to generate a foreign key to target column 'PGDASD_00000_ID_DECLARACAO'* 错误: *与列'pgdasd_01000.PGDASD_00000_ID_DECLARACAO'相关的外键找不到表'PGDASD',通过该表生​​成指向目标列'PGDASD_00000_ID_DECLARACAO'的外键

thanks! 谢谢!

Step 1: check that table(s) are created in db. 步骤1:检查已在db中创建表。 BTW How did you create these tables? 顺便说一句,您是如何创建这些表的? Have you run metadata.create_all() method ? 您是否运行过metadata.create_all()方法?

Step 2: Try to add the name of the schema to the ForeignKey defintion: 步骤2:尝试将架构名称添加到ForeignKey定义中:

Base = declarative_base()
SCHEMA = {'schema' : 'SIATDESV'}

class Pgdasd(Base):
    __tablename__ = 'PGDASD'
    __table_args__ = SCHEMA

    PGDASD_00000_ID_DECLARACAO    = Column(String(17), primary_key = True)

class Pgdasd_01000(Base):
    __tablename__ = 'pgdasd_01000'
    __table_args__ = SCHEMA

    PGDASD_00000_ID_DECLARACAO  = Column(String, ForeignKey('SIATDESV.PGDASD.PGDASD_00000_ID_DECLARACAO'))   
    PGDASD_01000_NRPAGTO        = Column(String, primary_key = True)

If this will help you may also make code to use schema value to avoid hardcoding the schema. 如果这样做会帮助您,还可以使代码使用模式值来避免对模式进行硬编码。

    PGDASD_00000_ID_DECLARACAO  = Column(String, ForeignKey(SCHEMA['schema']+'.PGDASD.PGDASD_00000_ID_DECLARACAO'))   

PS: Anyway it is always useful to check SQLAlchemy log what SQL queries it generates. PS:无论如何,检查SQLAlchemy日志总是有用的,它会生成什么SQL查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM