nopCommerce Bulk Insertion in Import using excel issue with Foreign Key - nopcommerce

The Insert statement conflicted with the foreign key 'ProductXXXXX'. The Conflict occurred in Database 'DatabaseName', table "dbo.TableName", column 'ColumnName'
The Statement has been terminated.
strong text

Related

I can't add foriegn key to my existing table. | sqlite3

So i am trying to complete finance. Following is the .schema:
sqlite> .schema
CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, username TEXT NOT NULL, hash TEXT NOT NULL, cash NUMERIC NOT NULL DEFAULT 10000.00);
CREATE TABLE sqlite_sequence(name,seq);
CREATE TABLE history(
symbol TEXT, name TEXT, shares INTEGER, price NUMERIC, time DATETIME
);
CREATE UNIQUE INDEX username ON users (username);
When i try to add foriegn key to history table it always return error. Here is my code:
sqlite> ALTER TABLE history ADD COLUMN id INT;
sqlite> ALTER TABLE history ADD FOREIGN KEY(id) REFRENCES users(id);
Parse error: near "FOREIGN": syntax error
ALTER TABLE history ADD FOREIGN KEY(id) REFRENCES users(id);
^--- error here
I think based on what I see in the sqlite docs that the statement should be together with the ADD column:
ALTER TABLE history ADD COLUMN id INTEGER REFERENCES users(id);
But you please check me on this syntax! Another option is to take care of creating the constraint at the same time that you create the table.
CREATE TABLE history(
symbol TEXT,
name TEXT,
shares INTEGER,
price NUMERIC,
time DATETIME,
id INTEGER,
FOREIGN KEY (id)
REFERENCES users (id));
It might not be something you have realized (yet) but every database has its unique flavor of SQL, so despite there being a SQL standard there are often little differences in the syntax of SQL for specific db implementations. So you always have to beware of this when looking up commands for your sql db.
Further detail on Sqlite foreign key constraints can be found here:
https://www.sqlitetutorial.net/sqlite-foreign-key/

SQLite3 insertion into foreign key table doesn't work

I have an SQLite3 database running on a Python Flask server. I am trying to insert values into foreign key database but the values for the foreign key are set at Null.
In my Flask application.py file:
from flask import Flask
from cs50 import SQL
app = Flask(__name__)
db = SQL("sqlite:///test.db")
db.execute("CREATE TABLE 'people' ('id' integer, 'name' varchar(100), PRIMARY KEY(id))")
db.execute("CREATE TABLE 'friendships' (person_id INTEGER,'id' smallint, 'friends' boolean, FOREIGN KEY(person_id) REFERENCES people(id))")
At some point in the code I want to insert a 'friendship' into the referenced database, assuming I have two people with ids 1 and 2 in the 'people' table. Therefore, I run the code:
db.execute("INSERT INTO friendships (person_id, id, friends) VALUES (1, 2, 1)")
This leaves me with a null value for this inserted row in the person_id field, and have no clue why it has happened. I'm also unable to find anything on similar problems online, so I would really appreciate any help.
Edit:
The code works on itself but I tried to insert an input from a non-existent form input value of person_id from the client-side to the server. This resulted in null values being submitted to the server, consequently ending up in the database.

I am insert one table ERP_GatePass but ERP_GatePass_old table will be Refer,So mthis Error Occur

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_ERP_AssetIssue_ERP_GatePass". The conflict occurred in database "ERP_ASSETS_NFA", table "dbo.ERP_GatePass_Old", column 'GPID'.
The statement has been terminated.
In your table dbo.ERP_GatePass, it has a foreign key reference to another table. The FK works is it cannot have a value in that column that is not also in the primary key column of the referenced table.
You can Rectify or findout using this following way Source

How do I import data into a table with composite key in SQLite?

So I have a table with a composite key defined as follows:
create table BOOK_REVIEW
(
ISBN varchar(13) NOT NULL,
ID_Num int NOT NULL,
Rating int NOT NULL,
Review varchar(8000),
primary key (ISBN, ID_Num),
foreign key (ISBN) references BOOK(ISBN)
ON DELETE cascade ON UPDATE cascade,
foreign key (ID_Num) references CONTACT(ID_Num)
ON DELETE cascade ON UPDATE cascade
);
The problem I'm having is that when I import data from a properly formatted csv file, I get the duplicate entry error. It's true that there are duplicate entries for ISBN, and there are duplicate entries for ID_Num in this table, but the composite key of (ISBN, ID_Num) is unique to each row. Why then am I getting the error?
I can obviously omit the primary key declaration and everything will import just fine, but I need the primary key to be defined correctly.
I've tried importing to a table without the primary key definition, then moving all data to a temp table, dropping the table and recreating with the primary key constraint, then trying to move everything back...but as expected the "duplicate entry" error pops again.
Any idea how to populate a table with a composite key by importing directly from a properly formatted csv file?

Creating foreign key on same table SQLite

Recently I started using SQLite (as required for my study) and I came accross a couple of restrictions of SQLite and I was wondering: can't SQLite create foreign keys on the same table? E.g. this is my code:
CREATE TABLE Categories
(
name varchar(20),
parent_category varchar(20) NULL,
PRIMARY KEY(name),
FOREIGN KEY parent_category_fk(parent_category) REFERENCES Categories(name)
)
But it gives me an error for the foreign key when I try to execute the SQL in SQLiteStudio.
Does anyone know why this isn't working?
The problem is that you have the wrong syntax for the FK clause. It should be:
FOREIGN KEY (parent_category) REFERENCES Categories(name)
If you want to name the FK constraint, you do that with a prefix of the CONSTRAINT keyword, like this:
CONSTRAINT parent_category_fk FOREIGN KEY (parent_category) REFERENCES Categories(name)

Resources