Reputation: 469
I have a simple Python script (models.py
) to create a Flask-SQLAlchemy app and a simple sqlalchemy dataclass (reference here) that looks like this:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///test.db"
db = SQLAlchemy(app)
class User(db.Model):
__tablename__ = 'user'
user_id: db.Column(db.Integer, primary_key=True)
user_url: db.Column(db.String(2083), unique=True, nullable=False)
username: db.Column(db.String(80), unique=True, nullable=False)
avatar_url: db.Column(db.String(2083), unique=True, nullable=False)
I then wrote an extremely simple pytest script that looks like this, following the example on creating a Flask-SQLAlchemy app from here:
import pytest
from models import User
from models import db
def test_basic():
db.create_all()
u1 = User(1, "http://testuserurl", "charliebrown", "http://avatarurl")
assert u1.user_id == 1
assert u1.user_url == "http://testuserurl"
assert u1.username == "charliebrown"
assert u1.avatar_url == "http://avatarurl"
This test fails immediately on the line "from models import User" with the message:
sqlalchemy.exc.ArgumentError: Mapper mapped class User->user could not assemble any primary key columns for mapped table 'user'
I don't understand this message (specially given that I do indeed specify the primary_key
in User
). Any help please?
Upvotes: 0
Views: 3579
Reputation: 34
Try to use =
instead of :
Like this:
class User(db.Model):
__tablename__ = 'user'
user_id = db.Column(db.Integer, primary_key=True)
user_url = db.Column(db.String(2083), unique=True, nullable=False)
username = db.Column(db.String(80), unique=True, nullable=False)
avatar_url = db.Column(db.String(2083), unique=True, nullable=False)
Upvotes: 2