Home > database >  SQLAlchemy script works fine with sqlite3 but raises errors when used with MYSQL
SQLAlchemy script works fine with sqlite3 but raises errors when used with MYSQL

Time:02-11

SQLAlchemy script works fine with sqlite3 but when connected to MYSQL database it gives different errors like:

  • AttributeError: '_NoResultMetaData' object has no attribute '_indexes_for_keys'
  • sqlalchemy.exc.NoSuchColumnError: Could not locate column in row for column 'filters.id'
  • KeyError: Column('id', Integer(), table=, primary_key=True, nullable=False)

The functions works fine when running separately but when used with flask they raise above errors.

My DB models:

from sqlalchemy import Column, Integer, String, MetaData, Boolean, DateTime
from sqlalchemy.ext.declarative import declarative_base

import datetime


Base = declarative_base()


class ProfileData(Base):
    __tablename__ = 'profile_data'

    id = Column(Integer, primary_key=True)
    user_id = Column("user_id", Integer)
    user_profile_link = Column(String(100))
    username = Column(String(100))
    name = Column(String(100))
    is_verified = Column(Boolean)
    website = Column(String(100))
    bio = Column(String(1000))
    location = Column(String(100))
    created_at = Column(DateTime)
    followers = Column(Integer)
    following = Column(Integer)
    _datetime = Column(DateTime, default=datetime.datetime.utcnow)

Thanks in advance.

CodePudding user response:

I would suggest to do this and this how usually do my setup

first init db object from SQLAlchemy

db = SQLAlchemy()

then declare the model using db.model

class ProfileData(db.Model):

    __tablename__ = 'profile_data'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column("user_id", db.Integer)
    user_profile_link = db.Column(db.String(100))
    username = db.Column(db.String(100))

hope this fix your issue

  • Related