sqlalchemy

Defining an abstract method in a SQLAlchemy base class

旧街凉风 提交于 2021-02-05 05:30:05
问题 From http://docs.sqlalchemy.org/en/improve_toc/orm/extensions/declarative/mixins.html#augmenting-the-base I see that you can define methods and attributes in the base class. I'd like to make sure that all the child classes implement a particular method. However, in trying to define an abstract method like so: import abc from sqlalchemy.ext.declarative import declarative_base class Base(metaclass=abc.ABCMeta): @abc.abstractmethod def implement_me(self): pass Base = declarative_base(cls=Base)

Defining an abstract method in a SQLAlchemy base class

强颜欢笑 提交于 2021-02-05 05:29:58
问题 From http://docs.sqlalchemy.org/en/improve_toc/orm/extensions/declarative/mixins.html#augmenting-the-base I see that you can define methods and attributes in the base class. I'd like to make sure that all the child classes implement a particular method. However, in trying to define an abstract method like so: import abc from sqlalchemy.ext.declarative import declarative_base class Base(metaclass=abc.ABCMeta): @abc.abstractmethod def implement_me(self): pass Base = declarative_base(cls=Base)

Defining an abstract method in a SQLAlchemy base class

爱⌒轻易说出口 提交于 2021-02-05 05:29:27
问题 From http://docs.sqlalchemy.org/en/improve_toc/orm/extensions/declarative/mixins.html#augmenting-the-base I see that you can define methods and attributes in the base class. I'd like to make sure that all the child classes implement a particular method. However, in trying to define an abstract method like so: import abc from sqlalchemy.ext.declarative import declarative_base class Base(metaclass=abc.ABCMeta): @abc.abstractmethod def implement_me(self): pass Base = declarative_base(cls=Base)

Using Flask-SQLAlchemy without Flask

风流意气都作罢 提交于 2021-02-04 22:09:36
问题 I had a small web service built using Flask and Flask-SQLAlchemy that only held one model. I now want to use the same database, but with a command line app, so I'd like to drop the Flask dependency. My model looks like this: class IPEntry(db.Model): id = db.Column(db.Integer, primary_key=True) ip_address = db.Column(db.String(16), unique=True) first_seen = db.Column(db.DateTime(), default = datetime.datetime.utcnow ) last_seen = db.Column(db.DateTime(), default = datetime.datetime.utcnow )

Using Flask-SQLAlchemy without Flask

吃可爱长大的小学妹 提交于 2021-02-04 22:01:33
问题 I had a small web service built using Flask and Flask-SQLAlchemy that only held one model. I now want to use the same database, but with a command line app, so I'd like to drop the Flask dependency. My model looks like this: class IPEntry(db.Model): id = db.Column(db.Integer, primary_key=True) ip_address = db.Column(db.String(16), unique=True) first_seen = db.Column(db.DateTime(), default = datetime.datetime.utcnow ) last_seen = db.Column(db.DateTime(), default = datetime.datetime.utcnow )

sqlalchemy orm column name convention

霸气de小男生 提交于 2021-02-04 21:45:16
问题 How do I automatically map all of my fields of the forms my_column to corresponding columns MyColumn in the database? class MyTable(Base): __tablename__ = 'MyTable' # what I have now: my_column_1 = Column('MyColumn1', Integer) # what I want: how to configure a convention mapping rule for this: my_column_1 = Column(Integer) 回答1: You could provide your own metaclass that renames columns, before passing control to DeclarativeMeta . Another option would be to alter column names in a before_parent

delete not cascaded to table in sqlalchemy

旧街凉风 提交于 2021-02-04 18:40:30
问题 I am developing an extension to an existing app which uses sqlalchemy 0.6. The app has sqlalchemy tables created the non-declarative way. I am trying to create in my extension a new table with a foreign key column pointing at the primary key of the main table in the application database and I am creating it declaratively. This all works fine, with the table created once the extension is loaded, and with no complaints at all. My table prints out and demonstrates that new rows have been added

How do I call a database function using SQLAlchemy in Flask?

非 Y 不嫁゛ 提交于 2021-02-04 15:13:28
问题 I want to call a function that I created in my PostgreSQL database. I've looked at the official SQLAlchemy documentation as well as several questions here on SO, but nobody seems to explain how to set up the function in SQLAlchemy. I did find this question, but am unsure how to compile the function as the answer suggests. Where does that code go? I get errors when I try to put this in both my view and model scripts. Edit 1 (8/11/2016) As per the community's requests and requirements, here are

How do I catch a psycopg2.errors.UniqueViolation error in a Python (Flask) app?

北城余情 提交于 2021-02-04 11:57:56
问题 I have a small Python web app (written in Flask) that uses sqlalchemy to persist data to the database. When I try to insert a duplicate row, an exception is raised, something like this: (psycopg2.errors.UniqueViolation) duplicate key value violates unique constraint "uix_my_column" I would like to wrap the exception and re-raise my own so I can add my own logging and messaging that is specific to that particular error. This is what I tried (simplified): from db import DbApi from my_exceptions

Concepts of backref and back_populate in SQLalchemy?

会有一股神秘感。 提交于 2021-02-04 10:37:06
问题 Can anyone explain the concepts of these two ideas and how they relate to making relationships between tables? I can't really seem to find anything that explains it clearly and the documentation feels like there's too much jargon to understand in easy concepts. For instance, in this example of a one to many relationship in the documentation: class Parent(Base): __tablename__ = 'parent' id = Column(Integer, primary_key=True) children = relationship("Child", back_populates="parent") class Child