Why doesn't init script create every table in the script? - docker

I am trying to make an init script for MariaDB, using docker-compose.
Only the 3 first tables are created, so player_team_relation and player table are not created.
create table if not exists user
(
user_id varchar(255) primary key,
username varchar(30) not null,
password_hash varchar(255) not null
);
create table if not exists history
(
history_id bigint auto_increment primary key,
user_id varchar(255) not null,
game_date timestamp not null,
constraint user_history_constraint foreign key (user_id) references user (user_id)
);
create table if not exists team
(
team_id bigint auto_increment primary key,
history_id bigint not null,
player_name varchar(30) not null,
score bigint not null,
hasWon bool not null,
constraint history_team_constraint foreign key (history_id) references history (history_id)
);
create table if not exists player_team_relation
(
player_id bigint not null primary key,
team_id bigint not null primary key,
constraint player_ptr_constraint foreign key (player_id) references player (player_id),
constraint team_ptr_constraint foreign key (team_id) references team (team_id)
);
create table if not exists player
(
player_id bigint auto_increment primary key,
user_id varchar(255) not null,
player_name varchar(30) not null,
wins int not null,
losses int not null,
score bigint not null,
constraint user_player_constraint foreign key (user_id) references user (user_id)
);
I have tried creating the database many times by deleting the volume and creating a new container.

Another example why error checking is always recommended:
The statement
create table if not exists player_team_relation
(
player_id bigint not null primary key,
team_id bigint not null primary key,
...
will fail, since it's not possible to define multiple primary keys per table. For more information on primary keys please check the documentation.
About your column definitions: Are you sure you want to use BIGINT (signed integer) instead of BIGINT UNSIGNED for auto_increment? Do you really need BIGINT? The maximum value of INTEGER UNSIGNED is 4294967295 - more than half of the world's population.

Related

Duplicate key value violates unique constraint in psql

Key (owner_id)=(0) already exists.
Getting the above error relating to the owners table when I try to run both tables, although the first table is created fine on its own. Any guidance would be greatly appreciated.
DROP TABLE pet CASCADE;
DROP TABLE owner CASCADE;
CREATE TABLE pet(
pet_id SERIAL4 primary key,
pet_type varchar(255),
pet_name varchar(255),
pet_age INT2,
pet_weight INT4,
behaviour_score INT4
);
CREATE TABLE owners(
owner_id SERIAL4 primary key,
owner_name varchar(255),
owner_email varchar(255)
);

Rails 3.1: Is there any way I can view which indexes have been created for my model?

I'm having a problem with my ActiveRecord exception handling and I suspect some of the indexes haven't been created as I thought they did. How can I view the indexes that have been created for my model?
Thanks so much in advance for your wisdom!
You can just look in your db/schema.rb where all the structure of the database is explicitly listed.
Assuming you are using MySQL, from your command line:
$ rails dbconsole
mysql> show create table users;
users | CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) DEFAULT '',
`email` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `index_users_on_email` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=61 DEFAULT CHARSET=latin1 |
Any "KEY" lines are your indexes.

Best DB grid to use with lots of lookup fields

I have a table with a couple foreign keys, something like this:
CREATE TABLE project.gl(
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
glaccount_id INT(11) UNSIGNED NOT NULL,
project_id INT(11) UNSIGNED NOT NULL,
booking_id INT(11) UNSIGNED NOT NULL,
cc_id INT(11) UNSIGNED DEFAULT NULL,
cu_id INT(11) UNSIGNED DEFAULT NULL,
PRIMARY KEY (id),
INDEX FK_gl_booking_id (booking_id),
INDEX FK_gl_cc_id (cc_id),
INDEX FK_gl_cu_id (cu_id),
INDEX FK_gl_glaccount_id (glaccount_id),
INDEX FK_gl_project_id (project_id),
CONSTRAINT FK_gl_booking_id FOREIGN KEY (booking_id)
REFERENCES project.booking (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT ......
I would like to display this in a DBGrid, but of course I don't want all the *_id's to show, but instead the data the id points to.
I don't want to use a query to resolve the id's, (for now) I want to use a plain TTable.
Which DBGrid variant would you recommend that can lookup foreign keys, using something like a DBLookupComboBox in the foreign key cell?
Just create a lookup field. Then the TDBGrid automatically shows a combobox for that column where you can select from the available values. The key field is automatically updated then.
See also TDBGridEh from Ehlib, it is more "light" than ExpressQuantumGrid but pretty powerful. It is a commercial product.
I do use the ExpressQuantumGrid from DeveloperExpress for this and many other tasks. It is very powerfull and mature and can have Lookup-Columns. But it is commercial and it is a very "heavy" component.

sqlite insert with primary key

I use sqlite(3.7.4) in iphone.
I create a table like:
create table A (UserName varchar (50) primary key, Num integer);
Then I insert the record below twice:
('abc',1);
Normally there should be only one record in DB.
However I found in the DB
(abc,1);
( ,1);
I'm confused that as UserName is primary key and why there are two records!
I don't know what's the problem.
Can anyone help me?
thank you.
Why you want use username as primary key. Primary key should unique identify record. What is why when you inserted twice in second time you have primary key constraint violation
create table A (UserName varchar (50) primary key, Num integer, unique (UserName));

How to insert primary key value explicitly?

I have a table called messages and here is the table structure, I don’t want id is auto increment field but it should be a primary key for that table.
Here is table structure for messages
CREATE TABLE `messages` (
`id` INT(11) NOT NULL,
`user_id` INT(11) NOT NULL,
`text` VARCHAR(255) NOT NULL,
`source` VARCHAR(100),
`created_at` DATETIME DEFAULT NULL,
`updated_at` DATETIME DEFAULT NULL,
PRIMARY KEY (`id`)
);
while insert the data into table I am using below hash object
msg['id'] = 12345;
msg['user_id'] = 1;
msg['text'] = 'Hello world';
If I save this hash into messages table, id is not inserting
message = Message.new(msg);
message.save!
Rails is building insert sql with out id, so id value is not inserting messages table.
How insert the id value in table, This the insert sql rails build with out using id field
INSERT INTO `users` (`updated_at`, `user_id `, `text`, `created_at`) VALUES('2010-06-18 12:01:05', '1', 'Hello world', '2010-06-18 12:01:05');
Setting ID value is often useful when migrating from legacy data or - as I am doing right now - merging two apps while preserving FK integrity.
I just scratched my head for a while and it seems you have to set the PK value before calling save. After the record is saved, ActiveRecord ignores #id= or update_attribute . So while setting up the record from an attribute hash I use:
article = Article.new(attrs)
article.id = attrs["id"]
article.save!
You're working against the way rails works. ActiveRecord reserves the use of the id column and manages it for you.
Why should id not be an auto-incrementing column if it's the primary key?
Why do you need to control its value?
If you need an id column you can control yourself, add another one. It won't be the primary key, but you can make it a unique index too.

Resources