R
R
Roman Morozov2021-01-11 01:23:40
MySQL
Roman Morozov, 2021-01-11 01:23:40

Flask-SQLAlchemy one-to-many relationship, how to solve foreign key problem?

Please tell me how to solve the problem:

2 exceptions come out when setting up a one-to-many connection

Can't find any foreign key relationships between '1c_items' and '1c_stock_info'.

Could not determine join condition between parent/child tables on relationship Items.stocks - there are no foreign keys linking these tables. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.


Items model code
class Items(db.Model):
    __tablename__ = '1c_items'

    id = db.Column(db.VARCHAR(100), primary_key=True, unique=True, nullable=False)
    manufacturer_id = db.Column(db.VARCHAR(100), nullable=False)  # Производитель
    price_group_id = db.Column(db.VARCHAR(100), nullable=True)  # Ценовая группа, может быть = NULL. Если NULL - цена по запросу
    code = db.Column(db.VARCHAR(50), nullable=True)  # КОд
    article = db.Column(db.VARCHAR(255), nullable=False)  # Артикул изделия
    name = db.Column(db.VARCHAR(500), nullable=False)  # Название изделия
    name_print = db.Column(db.VARCHAR(500), nullable=True)  # Название для печати
    type = db.Column(db.VARCHAR(255), nullable=True)  # ВидНоменклатуры
    storage_unit = db.Column(db.VARCHAR(50), nullable=True)  # Еденица хранения
    description = db.Column(db.Text, nullable=True)  # Описание
    volume = db.Column(db.Float, nullable=False)  # Объем
    volume_unit = db.Column(db.VARCHAR(50), nullable=True)  # ОбъемЕдиницаИзмерения
    weight = db.Column(db.Float, nullable=False)  # Вес
    weight_unit = db.Column(db.VARCHAR(50), nullable=True)  # ВесЕдиницаИзмерения
    length = db.Column(db.Float, nullable=False)  # Длина
    length_unit = db.Column(db.VARCHAR(50), nullable=True)  # ДлинаЕдиницаИзмерения
    image_url = db.Column(db.VARCHAR(255), nullable=True)  # Путь до изображению

    property_collection = db.Column(db.VARCHAR(255), nullable=True)  # Коллекция
    property_color = db.Column(db.VARCHAR(255), nullable=True)  # Цвет
    property_function = db.Column(db.VARCHAR(255), nullable=True)  # Функция
    property_protection = db.Column(db.VARCHAR(255), nullable=True)  # Степень защиты
    property_insert_color = db.Column(db.VARCHAR(255), nullable=True)  # Цвет вставки
    property_product = db.Column(db.VARCHAR(255), nullable=True)  # Изделие
    property_amperage = db.Column(db.VARCHAR(255), nullable=True)  # Номинальный ток
    property_status = db.Column(db.VARCHAR(255), nullable=True)  # Статус
    property_device_type = db.Column(db.VARCHAR(255), nullable=True)  # Тип устройства
    property_poles_count = db.Column(db.VARCHAR(255), nullable=True)  # Количество полюсов
    property_switching_off = db.Column(db.VARCHAR(255), nullable=True)  # Кривая отключения
    property_breaking = db.Column(db.VARCHAR(255), nullable=True)  # СвойствоОтключающаяСпособностьKA
    property_leakage_current = db.Column(db.VARCHAR(255), nullable=True)  # Ток утечки
    property_type = db.Column(db.VARCHAR(255), nullable=True)  # Тип
    property_direct_current = db.Column(db.VARCHAR(255), nullable=True)  # Постоянный ток

    stocks = db.relationship('StockInfo', backref='item', lazy="joined")

StocksInfo model code
class StockInfo(db.Model):
    __tablename__ = '1c_stock_info'

    id = db.Column(db.Integer, primary_key=True, nullable=False, autoincrement=True, unique=True)
    item_id = db.Column(db.VARCHAR(100), db.ForeignKey('1c_items.id'), nullable=False)  # Номенклатура ID
    party_id = db.Column(db.VARCHAR(100), nullable=False)  # Партия ID


The connection in the DB exists:
5ffb7e221a9ef464510860.png

But as a result, I get the above error when starting the application.

Here is the query that is being used:
item = db.session.query(Items).first()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Morozov, 2021-01-11
@WeRn-rm

I fixed the problem myself ... The error was in the configuration

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question