initial AUTO_INCREMENT value for TypeORM PrimaryGeneratedColumn - typeorm

Is it possible to set the initial AUTO_INCREMENT value for the table with TypeORM PrimaryGeneratedColumn? I tried to read the documentation and code but didn't find anything.

You cannot do it directly from typeorm. But there is nothing stopping you to achieve the same in a migration script. For example, you can tweak the schema of creation migration script like this
CREATE SEQUENCE public.my_seq
START WITH 1234
INCREMENT BY 1
NO MINVALUE
MAXVALUE 99999999
CACHE 1;
CREATE TABLE public."my_schema" (
number integer DEFAULT nextval('public.my_seq'::regclass) NOT NULL,
name character varying NOT NULL);
References:
https://github.com/typeorm/typeorm/issues/2153
https://learn.microsoft.com/en-us/sql/t-sql/statements/create-sequence-transact-sql?view=sql-server-ver16

Borrowed from the answer you can simply add to the migration script in the up section
await queryRunner.query(`ALTER TABLE \`tableName\` auto_increment=1000000`);

Related

Can we alter the dbspace of a informix table?

Suppose I have following schema.
create table tb1
(col1 Integer,
col2 varchar(50)
) in dbspace1 extent size 1000 next 500 lock mode row;
and I want to change the dbspace of above table to dbspace2 . After doing my alteration table schema should be look like as follows .
create table tb1
(col1 Integer,
col2 varchar(50)
) in dbspace2 extent size 1000 next 500 lock mode row;
Is it possible to do? If it is possible what is the command?
On the face of it, the ALTER FRAGMENT statement and the INIT clause allows you to write:
ALTER FRAGMENT ON TABLE tb1 INIT IN dbspace2;
The keyword TABLE is required; you could specify an index instead.
I've not actually experimented to prove that it works, but the syntax diagram certainly allows it.

MariaDB, DEFAULT CURRENT_TIMESTAMP not saving

I have a MariaDB version 10.2.13-MariaDB-10.2.13+maria~jessie, with a table ids. The table's create code is:
CREATE TABLE `ids` (
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`lastupdate` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, # for some reason this is being ignored
PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
;
When the table is created, the lastupdate column's default value is not set. In fact, looking at the CREATE code in HeidiSQL, I see ... DEFAULT '' ....
Furthermore, the following query runs without error, but does not affect the table
ALTER TABLE ids
MODIFY lastupdate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
whereas this one works completely fine
ALTER TABLE ids
MODIFY lastupdate TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00';
. What could be going wrong here?
Implementation detail: The database is being run inside a docker container trivially extended from the default mariadb image.
This is probably an issue with HeidiSQL (or this particular version of HeidiSQL), not an issue with MariaDB itself.
You can verify this by using the MariaDB client (mysql) and run your CREATE TABLE query and then:
SHOW CREATE TABLE ids;

Creating a PostgreSQL sequence to a field (which is not the ID of the record)

I am working on a Ruby on Rails app. We are using a PostgreSQL database.
There is a table named scores with the following columns:
Column | Type
--------------+-----------------------
id | integer
value | double precision
ran_at | timestamp
active | boolean
build_id | bigint
metric_id | integer
platform_id | integer
mode_id | integer
machine_id | integer
higher_better | boolean
job_id | integer
variation_id | integer
step | character varying(255)
I need to add a sequence to job_id (note: there is no model for job).
How do I create this sequence?
Use CREATE SEQUENCE:
CREATE SEQUENCE scores_job_id_seq; -- = default name for plain a serial
Then add a column default to scores.job_id:
ALTER TABLE scores ALTER COLUMN job_id SET DEFAULT nextval('scores_job_id_seq');
If you want to bind the sequence to the column (so it is deleted when the column is deleted), also run:
ALTER SEQUENCE scores_job_id_seq OWNED BY scores.job_id;
All of this can be replaced with using the pseudo data type serial for the column job_id to begin with:
Safely and cleanly rename tables that use serial primary key columns in Postgres?
If your table already has rows, you may want to set the SEQUENCE to the next highest value and fill in missing serial values in the table:
SELECT setval('scores_job_id_seq', COALESCE(max(job_id), 1)) FROM scores;
Optionally:
UPDATE scores
SET job_id = nextval('scores_job_id_seq')
WHERE job_id IS NULL;
How to check a sequence efficiently for used and unused values in PostgreSQL
Postgres manually alter sequence
How to reset postgres' primary key sequence when it falls out of sync?
The only remaining difference, a serial column is also set to NOT NULL. You may or may not want that, too:
ALTER TABLE scores ALTER COLUMN job_id SET NOT NULL;
But you cannot just alter the type of an existing integer:
ALTER TABLE scores ALTER job_id TYPE serial;
serial is not an actual data type. It's just a notational convenience feature for CREATE TABLE.
In Postgres 10 or later consider an IDENTITY column:
Auto increment table column
So I figured out how to do this using ActiveRecord migrations on Ruby on Rails. I basically used Erwin's commands and help from this page and put them in the migration files. These are the steps:
1.
In the terminal, type:
rails g migration CreateJobIdSequence
rails g migration AddJobIdSequenceToScores
2.
Edit the migration files as follows:
20140709181616_create_job_id_sequence.rb :
class CreateJobIdSequence < ActiveRecord::Migration
def up
execute <<-SQL
CREATE SEQUENCE job_id_seq;
SQL
end
def down
execute <<-SQL
DROP SEQUENCE job_id_seq;
SQL
end
end
20140709182313_add_job_id_sequence_to_scores.rb :
class AddJobIdSequenceToScores < ActiveRecord::Migration
def up
execute <<-SQL
ALTER SEQUENCE job_id_seq OWNED BY scores.job_id;
ALTER TABLE scores ALTER COLUMN job_id SET DEFAULT nextval('job_id_seq');
SQL
end
def down
execute <<-SQL
ALTER SEQUENCE job_id_seq OWNED BY NONE;
ALTER TABLE scores ALTER COLUMN job_id SET NOT NULL;
SQL
end
end
3.
Migrate the database. In the terminal type:
rake db:migrate

How to set auto increment by 1 on a table in your migration script

How to set auto increment by 1 on a table in your migration script
ALTER TABLE Table_name AUTO_INCREMENT = 1;
Is it possible to mention this during table creation or after that, in your DB migration scripts.
Maybe there are better solutions. Otherwise you can run raw SQL statements in migration scripts like this:
ActiveRecord::Base.connection.execute("ALTER TABLE Table_name AUTO_INCREMENT = 1")

Create missing auto increment attribute with rails migration

I'm writing a migration to convert a non-rails app into the right format for rails - one of the tables for some reason does not have auto increment set on the id column. Is there a quick way to turn it on while in a migration, maybe with change_column or something?
You need to execute an SQL statement.
statement = "ALTER TABLE `users` CHANGE `id` `id` SMALLINT( 5 ) UNSIGNED NOT NULL AUTO_INCREMENT"
ActiveRecord::Base.connection.execute(statement)
Note this is just an example. The final SQL statement syntax depends on the database.
If you're on postgesql, a single request won't make it. You'll need to create a new sequence in the database.
create sequence users_id_seq;
Then add the id column to your table
alter table users
add id INT UNIQUE;
Then set the default value for the id
alter table users
alter column id
set default nextval('users_id_seq');
Then populate the id column. This may be quite long if the table has many rows
update users
set id = nextval('users_id_seq');
Hope this helps postgresql users...
The Postgres answer by #jlfenaux misses out on the serial type, which does all of it for you automatically:
ALTER TABLE tbl add tbl_id serial;
More details in this related answer.

Resources