I am trying to create a table with the following query using the pg npm module (v7):
CREATE TABLE subscriptions(
id SERIAL PRIMARY KEY,
stripe_id VARCHAR(40) UNIQUE NOT NULL,
user INTEGER REFERENCES users,
plan VARCHAR(40) NOT NULL,
active BOOLEAN NOT NULL,
start DATE NOT NULL,
end DATE DEFAULT NULL
);
This seems to match the docs but it is throwing an error:
error: syntax error at or near "user"
The users table has a serial primary key for id, anyone know why this isn't working?
Edit: here's the docs for reference - https://www.postgresql.org/docs/9.4/static/ddl-constraints.html#DDL-CONSTRAINTS-FK
I'm using postgresql version 9.4.
user is a reserved keyword in postgresql. You may use any other column name in its place
Refer the postgresql documentation for the complete list of keywords - Key Words List
According to it, end is also reserved. So the last line of your code will generate an error
Related
I have the following link
when I copy paste the following syntax
-- Create DimProductCategory PK
ALTER TABLE [dbo].[DimProductCategory] WITH CHECK ADD
CONSTRAINT [PK_DimProductCategory_ProductCategoryKey] PRIMARY KEY CLUSTERED
(
[ProductCategoryKey]
) ON [PRIMARY];
GO
The syntax WITH CHECK ADD is not working. Also, many syntax from the document is not working wondering why it is not working on sql pool. is there an alternative way or any other documents related to this from azure.
That syntax will not work on Azure Synapse Analytics dedicated SQL pools and you will receive the following error(s):
Msg 103010, Level 16, State 1, Line 1 Parse error at line: 2, column:
40: Incorrect syntax near 'WITH'.
Msg 104467, Level 16, State 1, Line 1 Enforced unique constraints are
not supported. To create an unenforced unique constraint you must
include the NOT ENFORCED syntax as part of your statement.
The way to write this syntax would be using ALTER TABLE to add a non-clustered and non-enforced primary key, eg
ALTER TABLE [dbo].[DimProductCategory]
ADD CONSTRAINT [PK_DimProductCategory_ProductCategoryKey]
PRIMARY KEY NONCLUSTERED ( [ProductCategoryKey] ) NOT ENFORCED;
However as this table is a dimension, I would also suggest changing its distribution to REPLICATE, which you have to do in the table definition. So the whole statement would be something like:
CREATE TABLE [dbo].[DimProductCategory](
[ProductCategoryKey] [int] IDENTITY(1,1) NOT NULL UNIQUE NOT ENFORCED,
[ProductCategoryAlternateKey] [int] NULL,
[EnglishProductCategoryName] [nvarchar](50) NOT NULL,
[SpanishProductCategoryName] [nvarchar](50) NOT NULL,
[FrenchProductCategoryName] [nvarchar](50) NOT NULL
)
WITH (
DISTRIBUTION = REPLICATE,
CLUSTERED INDEX( [ProductCategoryKey] )
)
It will be a good exercise for you to convert the rest of the syntax in the lab. The foreign keys won't work either.
After creating a Shopware6 Entity with translations,
exactly following the official tutorial,
I get an Unknown column "<tablename>_id" error; the tablename seems to be interpreted as part of the column name.
There is a requirement on the naming scheme of mapped fields in the *_translation tables:
they HAVE to follow <tablename>_id. That is, the tablename IS part of the fieldname.
In the tutorial that's not obvious, since they speak of bundle_id, where "bundle" is not the name of the entity but the table name. Most likely you vendor-prefixed and modulename-prefixed your table.
Example:
Your Entity: ACME\CoolModule\Core\Content\FoobarDefinition
Entity Table:
acme_coolmodule_foobar
Translation-table HAS to be exactly:
CREATE TABLE IF NOT EXISTS `acme_coolmodule_foobar_translation` (
`acme_coolmodule_foobar_id` BINARY(16) NOT NULL,
`language_id` BINARY(16) NOT NULL, ...
The difficulty is that Shopware doesn't SWAG-Prefix their own tutorial-modules, so you can't see the difference.
I'm a beginner in Cassandra and I have a table like this:
CREATE TABLE Books(
Title text PRIMARY KEY,
Authors set<text>,
Family set <text>,
Publisher text,
Price decimal
);
(the other options are missing because it's only an example)
now I would like to execute this query:
DELETE Price FROM Books WHERE Authors CONTAINS 'J.K. Rowling' IF EXISTS;
But it doesn't work. I searched on Google but found nothing.
Hope somebody can help me and sorry if my english is not very good.
but it doesn't work.
That doesn't really give us enough information to help you. Usually, you'll want to provide an error message. I built your table locally, inserted data, and tried your approach. This is the error that I see:
InvalidRequest: Error from server: code=2200 [Invalid query]
message="Some partition key parts are missing: title"
DELETE requires that the appropriate PRIMARY KEY components be specified in the WHERE clause. In your case, Authors is not part of the PRIMARY KEY definition. Given the error message returned (and the table definition) specifying title is the only way to delete rows from this table.
aploetz#cqlsh:stackoverflow> DELETE FROM Books
WHERE title = 'Harry Potter and the Chamber of Secrets'
IF EXISTS;
[applied]
-----------
True
Can I do a query like this? UPDATE Books SET Family = Family + {'Fantasy'} WHERE Authors CONTAINS 'J.K. Rowling';
No. This fails for the same reason. Writes in Cassandra (INSERTs, UPDATEs, DELETEs are all writes) require the primary key (specifically, the partition key) in the WHERE clause. Without that, Cassandra can't figure out which node holds the data, and it needs that to perform the write.
I am unable to upsert a row using the datastax driver.
The data in the Cassandra table is stored like follows:
tag | partition_info
------------+--------------------------------------------------
sometag | {{year: 2018, month: 1}, {year: 2018, month: 2}}
tag is primary key and partition_info is a UDT
CREATE TYPE codingjedi.tag_partitions (
year bigint,
month bigint
);
I want that if a tag doesn't exist then it gets created. If tag exists then the new udt value gets appended to old one. I suppose I cannot use insert as it overrides previous value i.e. this will not work
QueryBuilder.insertInto(tableName).value("tag",model.tag)
.value("partition_info",setAsJavaSet(Set(partitionsInfo)))
I am trying to use update but it isn't working. Datastax driver gives error java.lang.IllegalArgumentException for following query
QueryBuilder.update(tableName).`with`(QueryBuilder.append("partition_info",setAsJavaSet(Set(partitionsInfo))))
.where(QueryBuilder.eq("tag", id.tag))
I tried using add and append for primary key but but got the error PRIMARY KEY part tag found in SET part
QueryBuilder.update(tableName).`with`(QueryBuilder.add("tag",id.tag))
.and(QueryBuilder.append("partition_info",setAsJavaSet(Set(partitionsInfo)))) .where(QueryBuilder.eq("tag", id.tag))
You're using the incorrect operation in your update statement - you're using append, but it's used to append data to columns of list types. You can use instead either add if you're adding a single value (your case, so you wont even need to wrap data into Set explicitly), or addAll if you're adding multiple values.
QueryBuilder.update(tableName)
.`with`(QueryBuilder.add("partition_info", partitionsInfo))
.where(QueryBuilder.eq("tag", id.tag))
my code looks like the sample code given below.
--table create statement
CREATE TABLE LOG
(
uniqueID NUMERIC(20,0) IDENTITY,
NAME VARCHAR(20) NOT NULL,
DESCRIPTION VARCHAR(200) NOT NULL,
USR VARCHAR(20) NOT NULL
)
--pojo class
public class Log
{
private long identifier;
private String name;
private String description;
private String user;
//getters+setters......
}
--insert statement in mapper
<insert id="insertRecord" parameterType="com.xxx.yyy.zzz.model.Log" useGeneratedKeys="true" keyProperty="identifier" keyColumn="uniqueID">
INSERT INTO LOG (NAME, DESCRIPTION, USR)
VALUES (#{log.name}, #{log.description}, #{log.user})
</insert>
issue: when i try to run this code against sybase database, am getting NullPointerException. When i tried to debug it, error came from within SybStatement.class. Sorry am not able to provide entier stacktrace due to constraint in copy/paste at my work station.
I am able to run the same code against H2 database successfully. Records got inserted and "identifier" in Log object is having the identify value same as database rows.
Did you face this issue in sybase?. Please share if anyone is having code for showing the usage of "useGeneratedKeys" mybatis feature in sybase..
Note:
I am running this insert statement using MybatisBatchItemWriter.
I tried to use two different sqlsessiontemplate objects for chunk reader & chunk writer and it didn't resolve the issue.
I am using jconn3 sybase jdbc jar, mybatis 3.4.4 and mybatis-spring 1.3.1 jar.
Thanks in advance
In SQL terms, you need to do SELECT ##IDENTITY to pick up the generated value. Thecquestion is if your framework generates such SQL...