rice
rice

Reputation: 53

TypeError: Column() got an unexpected keyword argument 'primary_key'

class Contacts(db.Model):
    """
    sno, name, email, phone_num, msg, date
    """
    sno = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), nullable=False)
    email = db.Column(db.String(20), nullable=False)
    phone_num = db.Column(db.String(12), nullable=False)
    msg = db.Column(db.String(120), nullable=False)
    date = db.Column(db.String(12), nullable=True)

Traceback (most recent call last): File "C:/Users/rithi/Flask_Blog/main.py", line 11, in class Contacts(db.Model): File "C:/Users/rithi/Flask_Blog/main.py", line 15, in Contacts sno = db.Column(db.Integer, primary_key=True) TypeError: Column() got an unexpected keyword argument 'primary_key'

Upvotes: 2

Views: 6983

Answers (3)

Shakeel Haider
Shakeel Haider

Reputation: 137

I got this error: TypeError: column() got an unexpected keyword argument 'nullable'

The reason was I wrote column() instead of Column(). look closer to your code it is just typo error..

Upvotes: 7

I've ran into the same problem just now. I am new to Python but I am going to try to summarize how I solved the problem for those who might have made the same mistake.

This is the code that I was writing and it showed 'Unresolved attribute reference 'Column' for class 'SQLAlchemy'' also for eg. 'Unresolved attribute reference 'Integer' for class 'SQLAlchemy' '.

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///new-books-collection.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = 
False
db = SQLAlchemy(app)


class Books(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(250), unique=True, nullable=False)
    author = db.Column(db.String(250), unique=True, nullable=False)
    rating = db.Column(db.Float, nullable=False)


db.create_all()

My problem was that I hovered over Column and Integer and clicked Add method Column() to class SQLAlchemy. Also the same with Integer.

From that moment the same errors came up for me because it created these empty methods in init.py of SQLAlchemy.

To solve this I reinstalled flask_sqlalchemy. pip uninstall flask_sqlalchemy and pip install flask_sqlalchemy. And did not Add methods again. It still showed Unresolved attribute reference, but the code ran with exit code 0 and the database was created, with the right data inside.

Hope that helps!

Upvotes: 0

kavili vishnu
kavili vishnu

Reputation: 15

I just started learning flask, and I faced the same problem. Just un-install the sqlalchemy by typing:- pip uninstall flask-sqlalchemy but navigating to your directory in the command prompt. It gets un-installed. Then install its again by getting into your project directory by typing:- pip install flask-sqlalchemy. For me, this one worked and I was able to start working with the database. Give it a try if you faced the same issue.

Upvotes: -1

Related Questions