SQLAlchemy正在为postgresql中的列生成序列,但没有启用序列.我怀疑我可能在发动机设置中做错了什么.

使用SQLAlchemy教程(http://docs.sqlalchemy.org/en/rel_0_9/orm/tutorial.html)中的示例:

#!/usr/bin/env python

from sqlalchemy import create_engine, Column, Integer, String, Sequence
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
    name = Column(String(50))
    fullname = Column(String(50))
    password = Column(String(12))

    def __repr__(self):
        return "<User(name='%s', fullname='%s', password='%s')>" % (
                                self.name, self.fullname, self.password)

db_url = 'postgresql://localhost/serial'
engine = create_engine(db_url, echo=True)
Base.metadata.create_all(engine)

使用此脚本,将生成下表:

serial=# \d+ users
                                 Table "public.users"
  Column  |         Type          | Modifiers | Storage  | Stats target | Description 
----------+-----------------------+-----------+----------+--------------+-------------
 id       | integer               | not null  | plain    |              | 
 name     | character varying(50) |           | extended |              | 
 fullname | character varying(50) |           | extended |              | 
 password | character varying(12) |           | extended |              | 
Indexes:
    "users_pkey" PRIMARY KEY, btree (id)
Has OIDs: no

但是,创建了一个序列was:

serial=# select sequence_schema,sequence_name,data_type from information_schema.sequences ;
 sequence_schema | sequence_name | data_type 
-----------------+---------------+-----------
 public          | user_id_seq   | bigint

SQLAlchemy 0.9.1、Python 2.7.5+、Postgresql 9.3.1、Ubuntu 13.10

-里斯

推荐答案

这是因为您为它提供了一个明确的Sequence.postgresql中的SERIAL数据类型生成其own序列,SQLAlchemy知道如何定位该序列——因此,如果省略Sequence,SQLAlchemy将呈现SERIAL,前提是该列是自动递增的(由autoincrement参数和Integer primary_键共同确定;它默认为True).但是当传递Sequence时,SQLAlchemy看到的意图是,您不希望SERIAL隐式创建序列,而是希望您指定的序列:

from sqlalchemy import create_engine, Column, Integer, String, Sequence
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class T1(Base):
    __tablename__ = 't1'

    # emits CREATE SEQUENCE + INTEGER
    id = Column(Integer, Sequence('user_id_seq'), primary_key=True)

class T2(Base):
    __tablename__ = 't2'

    # emits SERIAL
    id = Column(Integer, primary_key=True)

class T3(Base):
    __tablename__ = 't3'

    # emits INTEGER
    id = Column(Integer, autoincrement=False, primary_key=True)

engine = create_engine("postgresql://scott:tiger@localhost/test", echo=True)
Base.metadata.create_all(engine)

输出:

CREATE SEQUENCE user_id_seq

CREATE TABLE t1 (
    id INTEGER NOT NULL, 
    PRIMARY KEY (id)
)


CREATE TABLE t2 (
    id SERIAL NOT NULL, 
    PRIMARY KEY (id)
)


CREATE TABLE t3 (
    id INTEGER NOT NULL, 
    PRIMARY KEY (id)
)

Postgresql相关问答推荐

Postgres:如何优化在多个表上搜索列的相似性查询?(Pg_Trgm)

为什么我的应用程序接收的是空值而不是布尔值?

将XML解析从T-SQL迁移到Postgres时出现问题

SQLX::Query!带有UUID::UUID的宏在Rust中编译失败

在Postgre中的链接服务器上执行远程查询

在 postgres/presto/AWS-Athena 中,与 array_agg( (col1, col2) ) 相反的是什么来获得每组多行?

postgresql中多个左连接的空结果

如何计算每月的出版物数量?

如何判断上次在 TimescaleDB 上运行连续聚合作业(job)的时间

带有附加组的时间刻度 interpolated_average

Postgres 查询计划器不会使用更便宜的复合索引

如何包装 record_out() 函数?

使用间隔参数的 go postgres 准备好的语句不起作用

带有 postgres 的 DOCKER 容器,警告:could not open statistics file "pg_stat_tmp/global.stat": Operation not permitted

PostgreSQL 中是否有类似 zip() 函数的东西,它结合了两个数组?

为什么是||在 PostgreSQL/Redshift 中用作字符串连接

PostgreSQL:从转储中恢复数据库 - 语法错误

重命名 Amazon RDS 主用户名

PostgreSQL 9.1:如何连接数组中的行而不重复,加入另一个表

如何为本地 Rails 元素设置 Postgres 数据库?