3

I'm using Flask + Alembic + Sqlalchemy. I want to create two table and use it. First, i run alembic script :

"""add table insurace_bands and insurace_discounts

Revision ID: 39ba7ec3428
Revises: 18468fbedbac
Create Date: 2015-05-12 09:10:05.175513

"""

# revision identifiers, used by Alembic.
revision = '39ba7ec3428'
down_revision = '18468fbedbac'

from alembic import op
import sqlalchemy as sa

def upgrade():

        op.create_table('insurance_bands',

                sa.Column('id', sa.Integer(), nullable=False),

                sa.Column('name', sa.String(length=1024), nullable=False),

                sa.Column('product_price', sa.Numeric(precision=6, scale=2)),

                sa.Column('monthly_price', sa.Numeric(precision=6, scale=2)),

                sa.Column('active', sa.Boolean(), nullable=False, server_default='1'),

                sa.PrimaryKeyConstraint('id')
                )

    op.create_table('insurance_discounts',

                sa.Column('id', sa.Integer(), nullable=False),

                sa.Column('name', sa.String(length=1024), nullable=False),

                sa.Column('min_products', sa.Integer()),

                sa.Column('max_products', sa.Integer()),

                sa.Column('discount', sa.Numeric(precision=6, scale=2)),

                sa.Column('active', sa.Boolean(), nullable=False, server_default='1'),

                sa.PrimaryKeyConstraint('id')
                )

def downgrade():

    op.drop_table(
    'insurance_bands'
    )


    op.drop_table(
    'insurance_discounts'
    )

This script run ok. Then, i create two models like this:

# -*- coding: utf-8 -*-
"""
    admin.insurance_brands.models
    ~~~~~~~~~~~~~~~~~~~~~

    insurance brand models
"""

from ..core import db
from ..helpers import JsonSerializer


class InsuranceBandJsonSerializer(JsonSerializer):
    __json_public__ = ['id', 'name', 'product_price', 'monthly_price', 'active']


class InsuranceBand(InsuranceBandJsonSerializer, db.Model):

    __tablename__ = 'insurance_bands'

    id = db.Column(db.Integer(), primary_key=True),
    name = db.Column(db.Unicode(1024)),
    product_price = db.Column(db.Numeric(precision=6, scale=2)),
    monthly_price = db.Column(db.Numeric(precision=6, scale=2)),
    active = db.Column(db.Boolean, default=True)


class InsuranceDiscountJsonSerializer(JsonSerializer):
    __json_public__ = ['id', 'name', 'min_products', 'max_products', 'active']


class InsuranceDiscount(InsuranceDiscountJsonSerializer, db.Model):

    __tablename__ = 'insurance_discounts'

    id = db.Column(db.Integer(), primary_key=True),
    name = db.Column(db.Unicode(1024)),
    min_products = db.Column(db.Integer()),
    max_products = db.Column(db.Integer()),
    active = db.Column(db.Boolean, default=True)

But when i run server: python wsgi.py, it throws error:

sqlalchemy.exc.ArgumentError: Mapper Mapper|InsuranceBand|insurance_bands could not assemble any primary key columns for mapped table 'insurance_bands'

It seem model doesn't have primary key but i defined it.

Can anyone help me. Thanks in advance.

DonerKebab
  • 500
  • 4
  • 13

1 Answers1

3

In your classes, you must remove the , after )

class InsuranceBand(InsuranceBandJsonSerializer, db.Model):

    __tablename__ = 'insurance_bands'

    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.Unicode(1024))
    product_price = db.Column(db.Numeric(precision=6, scale=2))
    monthly_price = db.Column(db.Numeric(precision=6, scale=2))
    active = db.Column(db.Boolean, default=True)
damadam
  • 330
  • 4
  • 17
TC130
  • 31
  • 2
  • 1
    I had the same issue and your advise did help me. Thanks. It's strange, that Pycharm does not recognize this as mistake in syntax. – Anton Makarov Jul 26 '21 at 19:04