【问题标题】:No auto Increment when MySql Database converted to Postgres databaseMySql 数据库转换为 Postgres 数据库时没有自动增量
【发布时间】:2015-07-05 12:30:15
【问题描述】:

Mysql 中,IDAuto Increment,但转换为Postgres 时没有Auto Increment

Mysql 数据库

CREATE TABLE IF NOT EXISTS `cities` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `state_id` bigint(20) NOT NULL,
  `district_id` bigint(20) NOT NULL,
   `name` varchar(255) NOT NULL,
  `description` varchar(1000) DEFAULT NULL,
  `created_by` int(11) NOT NULL,
  `modified_by` int(11) DEFAULT NULL,
  `is_active` char(1) NOT NULL DEFAULT 'Y',
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `state_id` (`state_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=26 ;

转成postgres数据库后

CREATE TABLE cities (
    "id" bigint NOT NULL,
    state_id bigint NOT NULL,
    district_id bigint NOT NULL,
    "name" varchar(255) NOT NULL,
    description varchar(1000),
    created_by int NOT NULL,
    modified_by int,
    is_active char(1) NOT NULL,
    created timestamp,
    modified timestamp,
    PRIMARY KEY ("id")
);

INSERT INTO cities(state_id, district_id, city_type, name, description,  
     created_by, modified_by, is_active, created, modified) 
VALUES 
    (1, 1, '', 'Ramtek', null, 1, null, 'Y',
     '2015-04-16 10:44:11', '2015-04-16 10:44:11');

【问题讨论】:

标签: mysql database postgresql auto-increment


【解决方案1】:

在这种情况下,您可以使用bigserial。它在 postgres 中提供了自动增量功能:

CREATE TABLE cities (
   "id" bigserial PRIMARY KEY,

默认提供Not Null约束。

文档:http://www.postgresql.org/docs/current/static/datatype.html#DATATYPE-SERIAL

【讨论】:

  • 谢谢它有效,但有人怀疑何时使用 smallserial、bigserial 和 serial
  • 来自文档“类型名称 serial 和 serial4 是等价的:两者都创建整数列。类型名称 bigserial 和 serial8 的工作方式相同,除了它们创建一个 bigint 列。应该使用 bigserial 如果您预计在表的生命周期内使用超过 231 个标识符。类型名称 smallserial 和 serial2 也以相同的方式工作,只是它们创建了一个 smallint 列。"
  • 现在,当我尝试从前端添加数据时,它给出了一些数据库错误 SQLSTATE[23502]:非空违规:7 错误:列中的空值违反非空约束,当我更改我的代码时到“id” bigserial NOT NULL,
  • 你能发布你正在插入的查询吗?
  • 你也可以发布完整的日志吗?还有更新的城市 ddl
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-09
  • 1970-01-01
  • 2010-10-06
  • 2011-10-30
  • 2020-08-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多