logo
down
shadow

SQLAlchemy. How to use the mapper() without a primary key?


SQLAlchemy. How to use the mapper() without a primary key?

By : Rick Huizen
Date : November 21 2020, 09:01 AM
hope this fix your issue SQLAlchemy needs to know about a primary key, because it uses it to store it in it's identity-map. If a Table don't have a primary key, SQLAlchemy has no safe way to distinguish two object mapped to this Table.
Regarding your specific problem
code :


Share : facebook icon twitter icon
Sqlalchemy.exc.UnboundExecutionError: Could not locate a bind configured on mapper Mapper|SellsTable|sellers or this Ses

Sqlalchemy.exc.UnboundExecutionError: Could not locate a bind configured on mapper Mapper|SellsTable|sellers or this Ses


By : Celso
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I create a class for working with SQLAlchemy : , You should bind engine to your model.
code :
class DbAbsLayer(object):

    def createSession(self):
        Session = sessionmaker()
        self.session = Session.configure(bind=self.engine)
Python3 SQLAlchemy sqlalchemy.exc.UnboundExecutionError: Could not locate a bind configured on mapper Mapper or this Ses

Python3 SQLAlchemy sqlalchemy.exc.UnboundExecutionError: Could not locate a bind configured on mapper Mapper or this Ses


By : Snehal Shirgure
Date : March 29 2020, 07:55 AM
around this issue From "Using the Session":
code :
>>> from src.models import Question, Answer
>>> from src.database import db_session
>>> a = Answer("Yes")
>>> q = Question("Doctor Who?", a)
>>> s = db_session()
>>> s.add(a)
>>> s.add(q)
>>> s.commit()
Python and SQLAlchemy Classical Mapping using autoload: Mapper could not assemble any primary key columns for mapped tab

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


By : user6895984
Date : March 29 2020, 07:55 AM
hope this fix your issue
About database URI format: user name goes before user password, so it should be 'postgresql://someuser:somepassword@127.0.0.1/hello' in your example.
code :
from sqlalchemy import *
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import mapper

db = create_engine('postgresql://someuser:somepassword@127.0.0.1/hello')
metadata = MetaData(db)

# reflecting 'people' table from database schema
metadata.reflect(only=['people'])
people = metadata.tables['people']


class People(object):
    pass


people_mapper = mapper(People, people)

session_factory = sessionmaker(bind=db)
db_session = session_factory()

lewis_hamilton = People()
lewis_hamilton.name = 'Lewis'
lewis_hamilton.surname = 'Hamilton'
lewis_hamilton.age = 44

db_session.add(lewis_hamilton)
db_session.commit()
people = Table('people', metadata,
               Column('id', Integer, primary_key=True),
               Column('name', String(60)),
               Column('surname', String(60)),
               Column('age', SmallInteger))
from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

db = create_engine('postgresql://someuser:somepassword@127.0.0.1/hello')
metadata = MetaData(db)
people = Table('people', metadata,
               Column('id', Integer, primary_key=True),
               Column('name', String(60)),
               Column('surname', String(60)),
               Column('age', SmallInteger))

Base = declarative_base(metadata=metadata)


class People(Base):
    __table__ = people
from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

db = create_engine('postgresql://someuser:somepassword@127.0.0.1/hello')

metadata = MetaData(db)
Base = declarative_base(metadata=metadata)


class People(Base):
    __tablename__ = 'people'

    id = Column('id', Integer, primary_key=True)
    name = Column('name', String(60))
    surname = Column('surname', String(60))
    age = Column('age', SmallInteger)
from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

db = create_engine('postgresql://someuser:somepassword@127.0.0.1/hello')

metadata = MetaData(db)
Base = declarative_base(metadata=metadata)


class People(Base):
    __tablename__ = 'people'

    id = Column('id', Integer, primary_key=True)
    name = Column('name', String(60))
    surname = Column('surname', String(60))
    age = Column('age', SmallInteger)

    def __init__(self, name, surname, age):
        self.name = name
        self.surname = surname
        self.age = age


session_factory = sessionmaker(bind=db)
db_session = session_factory()

lewis_hamilton = People(name='Lewis',
                        surname='Hamilton',
                        age=44)

db_session.add(lewis_hamilton)
db_session.commit()
from sqlalchemy import *
sqlalchemy.exc.ArgumentError: Mapper mapped class could not assemble any primary key columns for mapped table 'users'

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


By : user3404014
Date : March 29 2020, 07:55 AM
this will help I am trying to connect my database. But I couldn't. , Remove each comma , in your class User, like below :
code :
class User(db.Model):
    __tablename__ = 'users'
    usr_id = db.Column(UUID(as_uuid=True), primary_key=True, server_default=text("uuid_generate_v4()"))
    usr_name = db.Column(db.String, nullable=False)
    usr_email = db.Column(db.String, nullable=False)
    usr_pass =  db.Column(db.String, nullable=False)

    def __repr__(self):
        return "<User(name='%s', email='%s')>" % (self.usr_name, self.usr_email)
"Class already has a primary mapper defined" error with SQLAlchemy

"Class already has a primary mapper defined" error with SQLAlchemy


By : Petit Moses
Date : March 29 2020, 07:55 AM
I wish did fix the issue. You are trying to map the same class Pheno to 2 different tables. SQLAlchemy allows only one primary mapper for each class, so that it knows what table to use for session.query(Pheno). It's not clear what do you wish to get from your question, so I can't propose solution. There are 2 obvious options:
define separate class to map to second table, create non-primary mapper for second table by passing non_primary=True parameter and pass it (the value returned by mapper() function) to session.query() instead of class.
Related Posts Related Posts :
  • Add and Subtract Times
  • torch.Tensor manipulation - Comparing two vectors
  • Clojure function that waits on the completion of another function before executing
  • Specman: How to find if a list of bytes exists in another list taking order of the list into account
  • Should I use docker-compose start or docker-compose up -d
  • is printf privileged instruction?
  • Sphinx arbitrary cross-reference destroys paragraph break?
  • Listing all `dynamic` variables for a class in Swift2
  • How to use SPARQL query to get field and field count at the same time?
  • slick carousel in grid mode cutting off bottom of second row
  • Checkbox submission to display text?
  • Using Presto on Cloud Dataproc with Google Cloud SQL?
  • Safari/Babel/Webpack Const declarations are not supported in strict mode
  • Unable to initialize firebase project using firebase-tools
  • gvNIX datatables set sorting options table (which fields)
  • How do you create a scrollable TW3ListMenu at run-time?
  • Visual Studio 2015 does not remember undocked windows position between debug and coding views
  • Data collection in Universal Recommender
  • phpexcel select cell after freezePane()
  • fullcalendar.io removeEventSource on dynamic events
  • Parsing string timestamp with time zone in 3-digit format followed by 'Z'
  • Ebay File Exchange Custom Field Removal or Rename
  • How to convert RelayJS connection to plain array?
  • Facebook api undefined issue
  • Issue creating stream definitions via rest interface
  • Why are there 2 registers in ARM for enabling/disabling interrupts instead of 1?
  • dnx ef is not recognized although defined in project.json
  • SHA-512 is it possible to get a collission in the first byte?
  • Cucumber jvm - specify multiple tags to ignore in After hook
  • Swift 2 run code if no exceptions
  • Why do we get to pick the source in an NP-completeness reduction?
  • Increment Number OnInsert()
  • How do you avoid repeating styles in shadow DOM?
  • Why would Capybara not allow fill_in when find for the same element works?
  • CKEditor's ckbuilder throws exception when build.sh is run
  • How do I make time$ work with ctrl+t e in ACL2 and emacs?
  • NVIC_SystemReset() stuck in while loop (STM32F302VB)
  • cypher retrieve nodes within a specific range time
  • Sharepoint Workflow Condtions
  • Display custom page when error happens in Koa
  • Python 3.5 [with tkinter buttons]
  • Rubymine 8: How to turn off Database Detector
  • Just getting started with Manatee.trello and encountering the following error
  • Adding advice to a mail inbound channel adapter
  • Digital Representation in different forms of videos
  • Add Ounces (oz) option to mass unit setting in Kentico
  • URL as barcode (not QR)
  • SonarQube analysis fails after upgrade to 3.7.1 rules
  • Creating Ratio with attributes of different dimensions
  • Python script not working if i run it using full path
  • Twilio - Using JavaScript?
  • Where can ratpack-jackson version 1.1.1 be resolved?
  • GCS slow upload from pod inside kubernetes GKE
  • type="number" use only CustomValidator
  • ErrorException in PlaceController.php line 101: Trying to get property of non-object - Laravel 5.1*
  • MDX - do no show contacts if the linked action is selected
  • Remove Right Column on Registration page
  • Angular UI Router: Dedicated UI-View
  • Maya 2015 Suggestions for speeding up exporting when scene has animation
  • pthreads, mq_timedreceive, pthread_cond_timedwait and CLOCK_REALTIME
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co