ArgumentError: Mapper mapped class Users->users could not assemble any primary key columns for mapped table 'users'

user9955506

I am new to SQLAlchemy. Trying to build database in Heroku Postgres

Get error: ArgumentError: Mapper mapped class Users->users could not assemble any primary key columns for mapped table 'users'

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import (Column, Integer, BigInteger, String, Sequence, TIMESTAMP, Boolean, JSON)
from sqlalchemy import sql
from sqlalchemy import Table, Column, Integer, String, MetaData, Sequence, create_engine
class Users(Base):

    __tablename__ = 'users'
    
    meta = MetaData()
    
    users_table = Table(
    'users', meta,
    Column('id', Integer, primary_key=True),
    Column('name',String(50)),
    Column('fullname',String(50)),
    Column('phone',Integer),
               )
       
    meta.create_all(engine)

    def __repr__(self):
        return "<User(id='{}', fullname='{}', username='{}')>".format(
            self.id, self.full_name, self.username)

Ian Wilson

The main reasons this does not work is that you try to create the table before the class User is created AND also the table you declare is not declared on the correct attribute. So sqlalchemy cannot find the id column. I have tried to outline a more idiomatic declarative approach below:


# This implicitly creates metadata, you do not need to make an explicit one.
Base = declarative_base()

class Users(Base):

    __tablename__ = 'users'
    
    # Using a table explicitly is not required here
    # Just declare the columns as properties.
    id = Column(Integer, primary_key=True)
    name = Column(String(50))
    fullname = Column(String(50))
    phone = Column(Integer)
       
    # Don't do this here because Users is not done 
    # meta.create_all(engine)

    def __repr__(self):
        return "<User(id='{}', fullname='{}', username='{}')>".format(
            self.id, self.full_name, self.username)

# Do this after classes are declared and use Base's metadata.
Base.metadata.create_all(engine)

References:

If you really want to set a table you have to set it with __table__ and use Base.metadata: https://docs.sqlalchemy.org/en/13/orm/extensions/declarative/table_config.html#using-a-hybrid-approach-with-table

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

sqlalchemy.exc.ArgumentError: Mapper mapped class could not assemble any primary key columns for mapped table 'users'

Mapper Mapper|User|users could not assemble any primary key columns for mapped table 'users'

Python and SQLAlchemy Classical Mapping using autoload: Mapper could not assemble any primary key columns for mapped table

Could not assemble any primary key columns for mapped table

Hibernate error - QuerySyntaxException: users is not mapped [from users]

Laravel change default primary key of users table

List All Users On Mapped Drive CMD

The column in table 'Users' do not match an existing primary key or unique constraint

ArgumentError in Users#index

Java Spark The requested route [/users/] has not been mapped in Spark for Accept: [*/*]

Primary Key for Users per Module? PostgreSQL

Laravel - store the value of primary key from persons table in the foreign key of users table

SQLAlchemy multiple foreign keys in one mapped class to the same primary key

SQLAlchemy multiple foreign keys in one mapped class to the same primary key

Are any of the columns in the Tableau Server sites or users customizable?

Is using SERIAL fine for the primary key when creating a table of all the comments made by users?

hibernate JPA error : org.hibernate.hql.internal.ast.QuerySyntaxException: users is not mapped

OTRS 6 - AD integration - Domain Admin users mapped as Agents, not as OTRS Admins

Can't Create Azure SQL Database Users Mapped to Azure AD Identities using Service Principal

Moving Columns for mobile users

Users as foreign key in Django

Same key for two users

SSH key for multiple users

MySQL Join Users table to Groups table, find users not in any of selected groups

Sorting a group of users among themselves in users table

How could you do to not show rows to users that are in a specific table

Separate Users class [Parse]

Creating USERs table in Express

Creating users table in Laravel