Reputation: 466
I'm trying to define a primary key in a table using SQLAlchemy. The problem is that I can't do it due to the following error:
ArgumentError: Mapper Mapper|BankAccount|bank_account could not assemble any primary key columns for mapped table 'bank_account'
My code is this one:
from flask import Flask,request,jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import os
# init app
app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
# Database
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' +
os.path.join(basedir,'db.sqlite')
app.config ['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# init db
db = SQLAlchemy(app)
# init marshmallow
ma = Marshmallow(app)
# class account
class BankAccount(db.Model):
number_acc: db.Column(db.String(50), primary_key = True)
owner_acc: db.Column(db.String(50))
bank: db.Column(db.String(50))
amount_acc: db.Column(db.Float)
# constructor of the class
def __init__(self,number_acc,owner_acc,bank,amount_acc):
self.number_acc = number_acc
self.owner_acc = owner_acc
self.bank = bank
self.amount_acc = amount_acc
How can I insert the primary key in the table bank_account
?
Upvotes: 2
Views: 267
Reputation: 52929
SQLAlchemy Column
objects are not type annotations, so you must assign them as class attributes instead – see "Declaring a Mapping" & "Mapping Table Columns":
class BankAccount(db.Model):
number_acc = db.Column(db.String(50), primary_key = True)
owner_acc = db.Column(db.String(50))
bank = db.Column(db.String(50))
amount_acc = db.Column(db.Float)
In your current attempt you effectively have no columns that Declarative can find during class construction, since the column information is hidden in the annotations.
Upvotes: 1