Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sqlalchemy foreign key problem
#1
hello im pretty stuck here, im trying to generate my models in order to autogenerate the tables and everyything works fine, but when i try to do something on my page im getting the following error

Error:
File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\flask\app.py", line 2309, in __call__ return self.wsgi_app(environ, start_response) File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\flask\app.py", line 2295, in wsgi_app response = self.handle_exception(e) File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\flask_restful\__init__.py", line 269, in error_router return original_handler(e) File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\flask_cors\extension.py", line 161, in wrapped_function return cors_after_request(app.make_response(f(*args, **kwargs))) File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\flask\app.py", line 1741, in handle_exception reraise(exc_type, exc_value, tb) File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\flask\_compat.py", line 34, in reraise raise value.with_traceback(tb) File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\flask\app.py", line 2292, in wsgi_app response = self.full_dispatch_request() File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request rv = self.handle_user_exception(e) File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\flask_restful\__init__.py", line 269, in error_router return original_handler(e) File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\flask_cors\extension.py", line 161, in wrapped_function return cors_after_request(app.make_response(f(*args, **kwargs))) File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\flask\app.py", line 1718, in handle_user_exception reraise(exc_type, exc_value, tb) File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\flask\_compat.py", line 34, in reraise raise value.with_traceback(tb) File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request rv = self.dispatch_request() File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\flask\app.py", line 1799, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\flask_restful\__init__.py", line 458, in wrapper resp = resource(*args, **kwargs) File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\flask\views.py", line 88, in view return self.dispatch_request(*args, **kwargs) File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\flask_restful\__init__.py", line 573, in dispatch_request resp = meth(*args, **kwargs) File "C:\Users\Ignacio Muriel\Desktop\programacion\comext\app\api\resources\data_stage.py", line 144, in post if data_stage_id not in [x.id for x in DataStage.query.all()]: File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 512, in __get__ mapper = orm.class_mapper(type) File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\sqlalchemy\orm\base.py", line 441, in class_mapper mapper = _inspect_mapped_class(class_, configure=configure) File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\sqlalchemy\orm\base.py", line 420, in _inspect_mapped_class mapper._configure_all() File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\sqlalchemy\orm\mapper.py", line 1337, in _configure_all configure_mappers() File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\sqlalchemy\orm\mapper.py", line 3229, in configure_mappers mapper._post_configure_properties() File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\sqlalchemy\orm\mapper.py", line 1947, in _post_configure_properties prop.init() File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\sqlalchemy\orm\interfaces.py", line 196, in init self.do_init() File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\sqlalchemy\orm\relationships.py", line 1861, in do_init self._setup_join_conditions() File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\sqlalchemy\orm\relationships.py", line 1941, in _setup_join_conditions can_be_synced_fn=self._columns_are_mapped, File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\sqlalchemy\orm\relationships.py", line 2246, in __init__ self._determine_joins() File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\sqlalchemy\orm\relationships.py", line 2361, in _determine_joins consider_as_foreign_keys=consider_as_foreign_keys, File "<string>", line 2, in join_condition File "<string>", line 2, in _join_condition File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\sqlalchemy\util\deprecations.py", line 130, in warned return fn(*args, **kwargs) File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\sqlalchemy\sql\selectable.py", line 918, in _join_condition a, a_subset, b, consider_as_foreign_keys File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\sqlalchemy\sql\selectable.py", line 999, in _joincond_scan_left_right col = fk.get_referent(b) File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\sqlalchemy\sql\schema.py", line 1880, in get_referent return table.corresponding_column(self.column) File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\sqlalchemy\util\langhelpers.py", line 855, in __get__ obj.__dict__[self.__name__] = result = self.fget(obj) File "C:\Users\IGNACI~1\Desktop\PROGRA~1\comext\app\venv\lib\site-packages\sqlalchemy\sql\schema.py", line 2025, in column tablekey, sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'clave_operacion_pedimento.clave_fecha_operacion_id' could not find table 'clave_fecha_operacion' with which to generate a foreign key to target column 'id'
so im not sure what is going on

here are my models, i have been trying a lot of things that i found on the web
from sqlalchemy import CHAR, Column, Date, ForeignKey, Integer, Numeric, String, text
from sqlalchemy.orm import relationship
from sqlalchemy.dialects.postgresql import BIT
from sqlalchemy.ext.declarative import declarative_base
from api.database import db, ma
from api.models import Model

Base = declarative_base()
metadata = Base.metadata



class ClaveDocumento(Model):
    __tablename__ = 'clave_documento'
    __table_args__ = {'schema': 'comext'}
    __sequencename__ = __tablename__ + '_id_seq'

    id = Column(
        Integer,
        db.Sequence(__sequencename__),
        primary_key=True,
        server_default=text("nextval('\""+__sequencename__+"\"'::regclass)")
    )
    clave = Column(String(25), nullable=False)
    descripcion = Column(String(250), nullable=False)
    def __init__(self, kwargs):
        self.__dict__.update(kwargs)

class ClaveFechaOperacion(Model):
    __tablename__ = 'clave_fecha_operacion'
    __table_args__ = {'schema': 'comext'}
    __sequencename__ = __tablename__ + '_id_seq'

    id = Column(
        Integer,
        db.Sequence(__sequencename__),
        primary_key=True,
        server_default=text("nextval('\""+__sequencename__+"\"'::regclass)")
    )
    tipo = Column(String(15), nullable=False)
    descripcion = Column(String(250), nullable=False)

    def __init__(self, kwargs):
        self.__dict__.update(kwargs)


class Regimen(Model):
    __tablename__ = 'regimen'
    __table_args__ = {'schema': 'comext'}
    __sequencename__ = __tablename__ + '_id_seq'

    id = Column(
        Integer,
        db.Sequence(__sequencename__),
        primary_key=True,
        server_default=text("nextval('\""+__sequencename__+"\"'::regclass)")
    )
    descripcion = Column(String(250), nullable=False)

    def __init__(self, kwargs):
        self.__dict__.update(kwargs)

class ClaveOperacionPedimento(Model):
    __tablename__ = 'clave_operacion_pedimento'
    __table_args__ = {'schema': 'comext'}
    clave = Column(CHAR(1), primary_key=True)
    fecha = Column(Date, nullable=False)
    clave_fecha_operacion_id = Column(ForeignKey("clave_fecha_operacion.id"), nullable=False)
    
    clave_fecha_operacion = relationship(ClaveFechaOperacion, foreign_keys=[clave_fecha_operacion_id])


    def __init__(self, kwargs):
        self.__dict__.update(kwargs)
    
    def __repr__(self):
        return '<Blog %r>' % self.name


class Pedimento(Model):
    __tablename__ = 'pedimento'
    __table_args__ = {'schema': 'comext'}
    __sequencename__ = __tablename__ + '_id_seq'

    id = Column(
        Integer,
        db.Sequence(__sequencename__),
        primary_key=True,
        server_default=text("nextval('\""+__sequencename__+"\"'::regclass)")
    )
    numero_pedimento = Column(String(7), nullable=False)
    tipo_operacion = Column(CHAR(1), nullable=False)
    fecha_pago_real = Column(Date, nullable=False)
    tipo_cambio = Column(Numeric(9, 5))
    patente = Column(String(4), nullable=False)
    regimen_id = Column(ForeignKey('regimen.id'), nullable=False)
    clave_documento_id = Column(ForeignKey("clave_documento.id"), nullable=False)
    seccion_aduanera_id = Column(ForeignKey("seccion_aduanera.id"), nullable=False)
    clave_operacion_pedimento_clave = Column(ForeignKey("clave_operacion_pedimento.clave"), nullable=False)

    clave_documento= relationship('ClaveDocumento', foreign_keys="[clave_documento_id]")
    clave_operacion_pedimento = relationship('ClaveOperacionPedimento', foreign_keys="[clave_operacion_pedimento_clave]")
    regimen = relationship('Regimen', foreign_keys=[regimen_id])
    seccion_aduanera = relationship('SeccionAduanera', foreign_keys="[seccion_aduanera_id]")

    def __init__(self, kwargs):
        self.__dict__.update(kwargs)

    def __repr__(self):
        return '<Blog %r>' % self.name


class SeccionAduanera(Base):
    __tablename__ = 'seccion_aduanera'
    __table_args__ = {'schema': 'comext'}
    __sequencename__ = __tablename__ + '_id_seq'
    id = Column(
        Integer,
        db.Sequence(__sequencename__),
        primary_key=True,
        server_default=text("nextval('\""+__sequencename__+"\"'::regclass)")
    )
    aduana = Column(String(2), nullable=False)
    seccion = Column(String(1), nullable=False)
    denominacion = Column(String(250), nullable=False)


Reply
#2
Quote:sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'clave_operacion_pedimento.clave_fecha_operacion_id' could not find table 'clave_fecha_operacion' with which to generate a foreign key to target column 'id'
It cannot find a table clave_fecha_operacion
See if the table exists or has a mistake in the name.
Reply
#3
i am also facing same problem any help
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Flask, When the associated foreign key is the same, it is a question of setting a uni passion044 0 931 Aug-02-2018, 08:29 AM
Last Post: passion044
Photo only foreign key id retrive when i pass model instance to form but i want other field x64 0 2,141 Jun-03-2017, 05:58 AM
Last Post: x64

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020