I have a User table in sqlite3 with schema:
CREATE TABLE "users" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"email" varchar(255) DEFAULT '' NOT NULL,
"encrypted_password" varchar(255) DEFAULT '' NOT NULL,
"admin" boolean,);
Which has a boolean field admin.
I add a user record from rails console:
User.create!({email: "example#example.com", encrypted_password: "sample string", admin:1})
Then when I query user record by the admin field
select * from users where admin=1;
It returns empty result set.
I had a look at sqlite users table, the admin field is saved as string 't' and 'f'.
This cause a problem, when I use custom query in rails, the admin filter is not compatible with sqlite3, which is my test database, and postgresql, which is my dev and production database.
How could I overcome this problem?
If you must use raw SQL, then you should use the same dbms in development, testing, and production. PostgreSQL will run fine on Windows, Linux, and OSX.
This is especially important when it comes to SQLite, which doesn't support data types at all in the way SQL databases do. SQLite allows this kind of literal nonsense.
sqlite> create table test (n integer not null);
sqlite> insert into test values ('wibble');
sqlite> select n from test;
wibble
But the query that's giving you trouble won't run in PostgreSQL anyway. This query
select * from users where admin=1;
will raise an error in PostgreSQL, because the integer 1 isn't a value in the domain of Boolean values. The string '1' is a valid value, though. So this will work.
select * from users where admin='1';
As will this.
select * from users where admin='t';
Related
I have created the following setup script. The script is meant to be run before the application (which would run on node) is deployed.
The idea is that it would eventually contain all table definitions, relations, stored procedures and some seed data to make the application work, split into different files.
But currently I'm stuck because I can't figure out how I can store data in variables.
In this example we have profiles (because user is a keyword) and inventories. I want each user to have an inventory. For this to work I need to generate an inventory, store its id in a variable and pass it to the profile.
But for some reason postgres won't allow me to declare variables. Heres what I got so far:
drop database test;
create database test;
\c test
create extension if not exists "uuid-ossp";
create table inventory(
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
name text
);
create table profile(
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
name text,
inventory_id uuid references inventory(id)
);
DECLARE myid uuid;
/*I want to set the inv_id to the result of this expression*/
insert into inventory(name) values('asdf') returning id into myid;
insert into profile(name,inventory_id) values ('user1',#myid)
But I get the following error:
$ psql -U postgres -f init.sql
psql:init.sql:18: ERROR: syntax error at or near "uuid"
LINE 1: DECLARE myid uuid;
^
So how can I create a variable to store this id? Am I doing something wrong in general, because I'm pretty sure Declare is part of the SQL spec.
You can use
WITH myids AS (
INSERT INTO inventory(name)
VALUES ('asdf')
RETURNING id
)
INSERT INTO profile(name,inventory_id)
SELECT 'user1', id
FROM myids;
I'm trying to store an entity in my postgresql database. This entity has a List in it, so I'd like to use postgresql type TEXT[]. But everytime I'm trying I get a SQL error, I have no idea why.
I don't get the syntax error, really. I'm sure it's a dumb issue but can you help me?
Thank you
I tried some alternatives, creating it directly from h2 console but I always get the same error
The script I use with flyway for creating the table
CREATE TABLE discrimination(
id SERIAL PRIMARY KEY NOT NULL ,
location VARCHAR(255) NOT NULL,
criteria TEXT[] NOT NULL,
domain VARCHAR(255) NOT NULL,
description TEXT NOT NULL,
name_organ VARCHAR(55) NOT NULL,
function_disc VARCHAR(55) NOT NULL
);
my application config for h2 & flyway
h2:
console:
enabled: true
path: /h2
datasource:
url: jdbc:h2:mem:formation-iris;MODE=PostgreSQL
username: test
password: test
driver-class-name: org.h2.Driver
flyway:
locations: classpath:db/migration
enabled: true
And the error I get
Syntax error in SQL statement "CREATE TABLE DISCRIMINATION(
ID SERIAL PRIMARY KEY NOT NULL ,
LOCATION VARCHAR(255) NOT NULL,
CRITERIA TEXT[[*]] NOT NULL,
DOMAIN VARCHAR(255) NOT NULL,
DESCRIPTION TEXT NOT NULL,
NAME_ORGAN VARCHAR(55) NOT NULL,
FUNCTION_DISC VARCHAR(55) NOT NULL
) "; expected "(, FOR, UNSIGNED, INVISIBLE, VISIBLE, NOT, NULL, AS, DEFAULT, GENERATED, ON, NOT, NULL, AUTO_INCREMENT, BIGSERIAL, SERIAL, IDENTITY, NULL_TO_DEFAULT, SEQUENCE, SELECTIVITY, COMMENT, CONSTRAINT, PRIMARY, UNIQUE, NOT, NULL, CHECK, REFERENCES, ,, )"; SQL statement:
From H2 documentation:
Compatibility Modes
For certain features, this database can emulate
the behavior of specific databases. However, only a small subset of
the differences between databases are implemented in this way.
Which means that H2 can emulate certain DB-specific behaviours, but it won't be fully compatible with the selected DB.
That's especially true for SQL syntax.
So, if you want to use arrays in H2, then you should use the H2 syntax ARRAY instead of TEXT[]
Which also means that you will need a separate SQL script for production (PostgreSQL) and for tests (H2). Luckily, flyway supports that. It can load the vendor-specific scripts from different folders. Extend the flyway configuration this way:
spring.flyway.locations=classpath:db/migration/{vendor}
and add the vendor-specific SQL scripts under the /h2 and /postgresql folders respectively.
I use knex to create a postgres table as following:
knex.schema.createTable('users', table => {
table.bigIncrements('user_id');
....
})
But after the table was created, the column user_id is a integer not the serial as expected.
The sql get by the pgAdmin is as following:
CREATE TABLE public.users
(
user_id bigint NOT NULL DEFAULT nextval('users_user_id_seq'::regclass),
....
)
And the consequence is that when I do insert statement, the user_id won't auto increment as expected.
Any gives?
====================
Currently I just changed to mysql connection, and the inserting works well. But if I changed the database back to postgresql, then inserting would fail due to the duplication of user_id. The code can be found here: https://github.com/buzz-buzz/buzz-service
serial and bigserial are not real types they are just shorthand for what pgAdmin is showing.
You will also find that a sequence has been created with the name users_user_id_seq when you look under sequences in pgAdmin.
I'm trying to migrate my sqlite database to postgresql.
I'm new to postgresql but stuck on this simple issue.
I fist used the pgloader tool to migrate to postgresql and have a working database. I see it created the table this way:
CREATE TABLE "AOrder"
(
"OrderNumber" bigserial NOT NULL,
"BuyerName" text,
etc
)
WITH (
OIDS=FALSE
);
ALTER TABLE "AOrder"
OWNER TO postgres;
Using pgadmin3 if I run the SQL query:
select
*
from AOrder
This returns the column names plus all the data as expected.
However, the following:
select
*
from "AOrder"
This returns just the column names and no data. Where's the data?
So it's not a problem with capitalization. Is there a setting in postgresql that is making this happen?
(This is ultimately the root problem for me since I am then using Sqlachemy which inserts the double quotes in queries. I could not figure out a way to change that behavior.)
Thanks!
I'm currently trying to automate a DB2 database through shell scripts but there seems to be a problem with the INSERT operation.
The tables I create look like this:
CREATE TABLE "TRAINING1"
(ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (STARTS WITH 1 INCREMENT BY 1),
F1 VARCHAR(20) NOT NULL,
name VARCHAR(40) NOT NULL,
surname VARCHAR(40) NOT NULL,
CONSTRAINT pk_train1_id primary key(ID));
Now the creation works just fine for all similar tables, but when I attempt to insert data:
db2 "insert into TRAINING1 values ('hello', 'world', 'foo', 'bar')"
I get this error:
SQL0117N The number of values assigned is not the same as the number
of specified or implied columns or variables. SQLSTATE=42802
As far as I understand, the primary key I specified should generate values automatically and cannot have values explicitly assigned to it. Out of curiosity I did this:
db2 "insert into TRAINING1 values (1, 'hello', 'world', 'foo', 'bar')"
and it then complains with this error:
SQL0798N A value cannot be specified for column "ID" which is defined
as GENERATED ALWAYS. SQLSTATE=428C9
I'm still fairly new to DB2 but almost a week later, I still haven't found any solutions to this yet.
I'm running DB2 Express Edition on a 64-bit Ubuntu virtual machine.
Any thoughts on why it's doing this?
Thanks
In order to skip over a column in an INSERT statement, you must specify the columns that are not to be skipped:
db2 "insert into TRAINING1 (f1, name, surname) values ('hello', 'world', 'foo')"