Python Forum
AttributeError: 'function' object has no attribute 'metadata
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AttributeError: 'function' object has no attribute 'metadata
#1
Hi!
Please help me with the error below.

Error:
Base.metadata.create_all(engine) AttributeError: 'function' object has no attribute 'metadata
repository -> users.py
from Model.Domain.users import Users
from sqlalchemy.orm import sessionmaker
from datetime import datetime

from Utils.utils import Base, engine


class DBUsersRepository:
    def __init__(self):
        self.session = sessionmaker(engine)()

    # Create new user
    def add_user(self, user_id, first_name, last_name, email_name, phone_number, address, date_of_birth, join_date):
        new_user = Users(
            user_id=user_id,
            first_name=first_name,
            last_name=last_name,
            email_name=email_name,
            address=address,
            phone_number=phone_number,
            date_of_birth=date_of_birth,
            join_date=join_date
        )

        self.session.add(new_user)
        self.session.commit()



if __name__ == '__main__':
    Base.metadata.create_all(engine)
    user_repo = DBUsersRepository()

    user_repo.add_user(
        user_id='1234567890123',
        first_name='fname',
        last_name='lname',
        email_name='[email protected]',
        address='address1',
        phone_number=757271432,
        date_of_birth='2000-22-10',
        join_date=datetime.now()
    )
Utils.py
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine

Base = declarative_base

engine = create_engine('mysql+pymysql://root@localhost:3306/BankApp')
domain -> users.py
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, String, Integer
from sqlalchemy.types import DateTime

Base = declarative_base()


class Users(Base):

    __tablename__ = 'users'

    user_id = Column(String(13), primary_key=True)
    first_name = Column(String(30), nullable=False)
    last_name = Column(String(30), nullable=False)
    email_name = Column(String(50), nullable=False)
    address = Column(String(50), nullable=False)
    phone_number = Column(String(10), nullable=False)
    date_of_birth = Column(DateTime, nullable=False)
    join_date = Column(DateTime, nullable=False)

    def __repr__(self):
        return f'user id = {self.user_id}\n' \
               f'first name = {self.first_name}\n' \
               f'last name = {self.last_name}\n' \
               f'email = {self.email_name}\n' \
               f'phone = {self.phone_number}\n' \
               f'date of birth = {self.date_of_birth}\n' \
               f'join date = {self.join_date}'
Reply
#2
In utils.py you have this:
Base = declarative_base
declaritive_base is a function, so Base = declaritive_base is a function. Base = declaritive_base() calls the function and assigns the result to Base.
Reply
#3
(Mar-27-2022, 08:11 PM)deanhystad Wrote: In utils.py you have this:
Base = declarative_base
declaritive_base is a function, so Base = declaritive_base is a function. Base = declaritive_base() calls the function and assigns the result to Base.

You're right, I got rid of the first mistake.
But now I'm getting another error

Error:
sqlalchemy.exc.ProgrammingError: (pymysql.err.ProgrammingError) (1146, "Table 'BankApp.users' doesn't exist") [SQL: INSERT INTO users (user_id, first_name, last_name, email_name, address, phone_number, date_of_birth, join_date) VALUES (%(user_id)s, %(first_name)s, %(last_name)s, %(email_name)s, %(address)s, %(phone_number)s, %(date_of_birth)s, %(join_date)s)] [parameters: {'user_id': '1234567890123', 'first_name': 'fname', 'last_name': 'lname', 'email_name': '[email protected]', 'address': 'address1', 'phone_number': 757271432, 'date_of_birth': '2000-22-10', 'join_date': datetime.datetime(2022, 3, 28, 8, 27, 3, 742772)}] (Background on this error at: https://sqlalche.me/e/14/f405)
Reply
#4
(Mar-28-2022, 05:31 AM)3lnyn0 Wrote: (1146, "Table 'BankApp.users' doesn't exist")
I think this means you should first create the table "users" in schema "BankApp".
Reply
#5
(Mar-28-2022, 07:48 AM)ibreeden Wrote:
(Mar-28-2022, 05:31 AM)3lnyn0 Wrote: (1146, "Table 'BankApp.users' doesn't exist")
I think this means you should first create the table "users" in schema "BankApp".

That's why I use base.metadata to automatically create my tables.
If I create them I don't use the base anymore.
Reply
#6
I have a tutorial here: https://python-forum.io/thread-24127.html
take a look at the model in the tutorial
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  getpass.getpass() results in AttributeError: module 'os' has no attribute 'O_NOCTTY' EarthAndMoon 4 765 Oct-03-2023, 02:00 PM
Last Post: deanhystad
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 1,674 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
Question image manipulation (cropping and MetaData) SpongeB0B 4 1,154 Jul-03-2023, 06:35 PM
Last Post: SpongeB0B
  Python: Regex is not good for re.search (AttributeError: 'NoneType' object has no att Melcu54 9 1,490 Jun-28-2023, 11:13 AM
Last Post: Melcu54
  Parallel processing - AttributeError: Can't get attribute 'sktimekmeans' Mohana1983 1 748 Jun-22-2023, 02:33 AM
Last Post: woooee
  Python: AttributeError: 'PageObject' object has no attribute 'extract_images' Melcu54 2 3,872 Jun-18-2023, 07:47 PM
Last Post: Melcu54
  Object attribute behavior different in 2 scripts db042190 1 730 Jun-14-2023, 12:37 PM
Last Post: deanhystad
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,337 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,598 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  WebDriver' object has no attribute 'find_element_by_css_selector rickadams 3 5,902 Sep-19-2022, 06:11 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020